🚀欢迎互三👉:WSH2012ffff 💎💎
🚀关注博主,后期持续更新系列文章
🚀如果有错误感谢请大家批评指出,及时修改
🚀感谢大家点赞👍收藏⭐评论✍
目录
在C++程序开发中,也会遇到很多文件上传,文件写入等对于文件的操作业务需要开发,文件处理也是任何应用程序的重要组成部分。C++有几种创建,读取,更新和删除文件的方法。本文主要介绍C++ File文件操作常用的函数方法。
1、常用的文件打开方式(打开模式标记)
注意:可以用“或”把以上属性连接起来,如,
ios::out|ios::binary
。文件的打开模式标记代表了文件的使用方式,这些标记可以单独使用,也可以组合使用。
2、C++ 文件支持文件的输入输出的类
1)ofstream:写操作,输出文件类。
2)ifstream:读操作,输入文件类
3)fstream:可同时读写的文件类。
3、文件打开与关闭相关函数方法
1)文件打开函数
ofstream
、ifstream
、fstream
都有open
成员函数:void open(const char* szFileName, int mode)
szFileName
参数是指向文件名的指针,mode
参数是文件的打开模式标记。例如,
#include <iostream> #include <fstream> using namespace std; int main() { ifstream inFile; inFile.open("c:\\tmp\\test.txt", ios::in); if (inFile) //true说明文件打开成功 inFile.close(); else cout << "test.txt doesn't exist" << endl; ofstream oFile; oFile.open("test1.txt", ios::out); if (!oFile) //true说明文件打开出错 cout << "error 1" << endl; else oFile.close(); oFile.open("tmp\\test2.txt", ios::out | ios::in); if (oFile) //true说明文件打开成功 oFile.close(); else cout << "error 2" << endl; fstream ioFile; ioFile.open("..\\test3.txt", ios::out | ios::in | ios::trunc); if (!ioFile) cout << "error 3" << endl; else ioFile.close(); return 0; }2)文件关闭函数
调用
open()
方法打开文件,是文件流对象和文件之间建立关联的过程。调用close()
方法关闭已打开的文件,该文件流并会被销毁,其后续还可用于关联其它的文件。语法:
void close( )
例如,
#include <fstream> using namespace std; int main() { const char *url="www.cjavapy.com"; ofstream outFile("site.txt", ios::out); //向 url.txt 文件中写入字符串 outFile.write(url, 30); //关闭已打开的文件 outFile.close(); return 0; }3) ofstream::is_open
bool is_open()
:文件打开返回 true ,否则 false
4、文件读取函数方法
C++输入文件流
ifstream
的继承关系:ios_base <- ios <- istream <- ifstream
1)istream::get()
istream& get (char& c):从
cin
中输出一个字符,如cin.get(c)
。istream& get (char* s, streamsize n):从
cin
或者istream
中提取c风格的字符串,最大字符串长度为n
,最后一个字符为‘\0’
字符串结束字符。例如:
char c[20]; cin.get(c,20);istream& get (char* s, streamsize n, char delim):
delim
为定界字符(delimiting characters),其默认值应该是'\n'
(换行字符),如果人为定义,可以是任意定义的字符,例如,
cin.get(c,20,'a');//当遇到a字符时,只取a字符前的字符串部分输出给c字符串,此时,a字符并不从cin输入缓冲区提取,而是后来的变量提取,而getline则直接丢弃该字符;istream& ignore (streamsize n = 1, int delim = EOF):丢弃多
n
个字符,或者遇到dlim
为止。2)istream::getline
istream& getline (char* s, streamsize n ):读取一行到字符数组。默认
delim
是换行字符'\n'
,遇到后丢弃,第二次读取从delim
后开始读。istream& getline (char* s, streamsize n, char delim ):自己定义停止符
delim
3)std::getline (string)
//用户定义截止字符 istream& getline (istream& is, string& str, char delim); istream& getline (istream&& is, string& str, char delim); //c++11 标准 //截止字符默认'\n' istream& getline (istream& is, string& str); istream& getline (istream&& is, string& str); // c++11 标准从流对象is中读取一行存到字符串str 直到遇到截止字符,如果遇到截止字符,则把它从流中取出来,然后丢弃(它不被存储,下一个操作的起点在它之后)函数调用前str 中的内容将被覆盖。
例如,
#include<iostream> #include<fstream> #include<string> using namespace std; int main() { string str; ifstream ifs("test.txt"); if(!ifs){ cout<<"open file fail!"<<endl; return 1; } while( getline(ifs,str)) { cout<<str<<endl; } return 0; }4)istream::read
istream& read (char* s, streamsize n):从输入流中提取n个字符,并把他们存数组
s
中,不检测内容,也不加字符串结尾符号‘\0’
。例如,
#include <iostream> // std::cout #include <fstream> // std::ifstream int main () { std::ifstream is ("test.txt", std::ifstream::binary); if (is) { // get length of file: is.seekg (0, is.end); int length = is.tellg(); is.seekg (0, is.beg); char * buffer = new char [length]; std::cout << "Reading " << length << " characters... "; // read data as a block: is.read (buffer,length); if (is) std::cout << "all characters read successfully."; else std::cout << "error: only " << is.gcount() << " could be read"; is.close(); // ...buffer contains the entire file... delete[] buffer; } return 0; }5)istream::putback
istream& putback (char c):从输入流读取一个字符,再把它返回。
例如,
char c = std::cin.get(); std::cin.putback (c);6)istream::unget
istream& unget():返回最后一次读取的字符到输入流,类似
putback()
。例如,
char c = std::cin.get(); std::cin.unget();
5、文件写入函数方法
C++输出文件流
ofstream
的继承关系:ios_base <- ios <- ostream <- ofstream
1)std::ostream::operator<<
用法和
cout <<
相同 ,将数据写入到文件最方便的函数,重载了常用的数据类型。arithmetic types (1)
ostream& operator<< (bool val);
ostream& operator<< (short val);
ostream& operator<< (unsigned short val);
ostream& operator<< (int val);
ostream& operator<< (unsigned int val);
ostream& operator<< (long val);
ostream& operator<< (unsigned long val);
ostream& operator<< (long long val);
ostream& operator<< (unsigned long long val);
ostream& operator<< (float val);
ostream& operator<< (double val);
ostream& operator<< (long double val);
ostream& operator<< (void* val);
stream buffers (2)
ostream& operator<< (streambuf* sb );
manipulators (3)
ostream& operator<< (ostream& (*pf)(ostream&));
ostream& operator<< (ios& (*pf)(ios&));
ostream& operator<< (ios_base& (*pf)(ios_base&));2)ostream::put
ostream& put (char c):插入字符
c
到流中。3)ostream::write
ostream& write (const char* s, streamsize n):从数组
s
中取n
个字符插入到流中。例如,
#include <iostream> #include <fstream> using namespace std; int main(){ //1.ofstream写入out.txt const char * filename = "test.txt"; string end= "123456"; ofstream out("out.txt"); if(out.is_open()) { /* "<<":插入器,向流输出数据. ">>":析取器,向流输出数据. */ out<<"Hello World." << filename << " "<< end << endl; out.close(); } //2.读取out.txt ifstream in("out.txt"); char buffer[200]; if(in.is_open()) { while(!in.eof()) { in.getline(buffer, 100); cout << buffer << endl; out.close(); } } return 0; }
6、其它函数
1)istream::ignore
istream& ignore (streamsize n = 1, int delim = EOF):从输入流中读取
n
个字符并且丢弃,或者读取到delim
字符再停止读取。2)istream::peek
int peek():返回输入流下一个字符,并把它留在输入流中,作为下一次读取的起点。返回值是整形ascll码值,可以用
char(c)
转化为字符。3)istream::tellg
streampos tellg():读取输入流中文件指针的位置,返回值可转化为
int
。例如,
is.seekg (0, is.end); int length = is.tellg(); is.seekg (0, is.beg)4)istream::seekg
istream& seekg (streampos pos),istream& seekg (streamoff off, ios_base::seekdir way):参数
pos
是流中的绝对位置可以转化为int
参数off
是偏移量,与way
相关,类型是int
参数way
可以选下表中的任意一个常量。5)ios::rdstate
iostate rdstate():返回当前流中的内部错误状态,
iostate
二进制数,需要做位运算来获取其相应位置上的值。这个函数的功能可以被good()
,eof()
,fail()
,bad()
替换。例如,
#include<iostream> #include<fstream> #include<string> using namespace std; int main () { std::ifstream is; is.open ("test.txt"); if ( (is.rdstate() & std::ifstream::failbit ) != 0 ) std::cerr << "Error opening 'test.txt'\n"; return 0; }6)ios::good
bool good() const;
bool eof() const;
bool fail() const;
bool bad() const;检测流的状态是否正常。当错误的状态flags (eofbit, failbit 和 badbit) 都没被设置的时候返回
true
特定的错误状态可以用下面的函数(
eof
,fail
, 和bad
)来检测。7)ios::operator!
有错误状态返回 true
例如,
#include<iostream> #include<fstream> #include<string> using namespace std; int main () { std::ifstream is; is.open ("test.txt"); if (!is) std::cerr << "Error opening 'test.txt'\n"; return 0; }8)ios::operator bool
布尔运算: 当流对象单独出现在条件语句中时,就间接调用布尔运算。
如:
if(ios)
,while(ios)
。c++98:
operator void*() const;
c++11:
explicit operator bool() const;
布尔运算一个很方便的用法就是检测文件结束。读到文件末尾的时候, eofbit, failbit 同时被设置为1,所以可以使用
bool()
来判断流的状态。当文件打开失败的时候failbit 位被设置为1,所以也能检测打开是否成功。
例如,
#include<iostream> #include<fstream> #include<string> using namespace std; void print_state (const std::ios& stream) { cout << "good()=" << stream.good(); cout << " eof()=" << stream.eof(); cout << " fail()=" << stream.fail(); cout << " bad()=" << stream.bad()<<endl; } int main() { string str; ifstream ifs("test.txt"); if(ifs) { //while( bool(getline(ifs,str)))// 等价 //while( getline(ifs,str).good())//等价 while( getline(ifs,str)) { cout<<"line:"<<str<<endl; } } else{ cout<<"open file fail!"<<endl; return 1; } print_state(ifs); return 0; }