• C++文件的操作


    文件和流

    C++把每一个文件都看成一个有序的字节流,每一个文件都以EOF作为文件结束符。

    文件的打开与关闭

    文件流分为三类:输入文件流、输出文件流、I/O文件流,对应为ifstream、ofstream和fstream。

    ifstream ifile;	//声明一个输入流
    ofstream ofile;	//声明一个输出流
    fstream iofile;	//声明一个输入/输出流
    
    • 1
    • 2
    • 3

    生命文件流之后就可以用open()函数打开。

    void open (const char* filename,int mode, int prot=filebuf::openprot);
    
    • 1
    参数: 
    filename    操作文件名
    mode        打开文件的方式
    prot        打开文件的属性
    
    • 1
    • 2
    • 3
    • 4

    mode取值必须是以下值之一:

    ios::in	    打开输入文件。将这种方式应用于ofstream,可以使现存文件内容不被清除。
    ios::out	打开输出文件。将这种方式应用于ofstream,而且没有使用ios::app、ios::ate或ios::in时,意味着使用了ios::trunc模式(如果文件存在则清楚原文件内容)。
    ios::ate	打开已存在文件,并指向末尾
    ios::app	打开一个仅用于添加的输出文件
    ios::trunc	如果文件已存在则先删除该文件
    ios::nocreate 如果文件不存在则打开失败,不创建
    ios::noreplace 如果文件存在则打开失败
    ios::binary	二进制方式打开文件
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    对于ifstream默认的mode参数是ios::in,对于ofstream默认的mode参数是ios::out。参数mode的符号常量通常可以组合使用,如:ios::binary|ios::in,表示只读方式打开一个二进制文件。

    参数prot的取值如下:

    0 普通文件
    1 只读文件
    2 隐含文件
    4 系统文件
    
    • 1
    • 2
    • 3
    • 4

    调用方式:

    ifstream ifile;
    ifile.open("c:\\vc\\abc.txt", ios::ate);
    
    • 1
    • 2
    ofstream ofile;
    ofile.open("c:\\vc\\abc.txt", ios::ate);
    
    • 1
    • 2

    或者

    ifstream ifile("c:\\vc\\abc.txt", ios::ate);
    
    • 1
    ofstream ofile("c:\\vc\\abc.txt", ios::ate);
    
    • 1

    判断文件是否打开成功

    ifstream ifile;
    ifile.open("c:\\vc\\abc.txt", ios::ate);
    if(!ifile)
    {
    //打开失败
    }
    //继续正常操作
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    文本文件的读写

    文本文件只得是以字符方式存放的数据,只使用于解释为ASCII码的文件。

    读文本文件的几种方法

    (1)使用流运算符>>直接读取
    这个符号将完成文件的字符转换工作,使用流运算符>>读取字符时,遇到换行或者空格将终止。

    #include 
    #include 
    using namespace std;
    
    int main()
    {
    	char str[12];
    	ifstream ifile("C:\\VS2013Projects\\test.txt", ios::in);
    	if (!ifile)
    	{
    		cout << "test文件不能打开" << endl;
    		return 0;
    	}
    
    	ifile >> str;
    	cout << "str:" << str << endl;
    	ifile.close();
    	return 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    (2)使用流成员函数读取文件数据
    常用的流成员函数有get、getline和read。get功能与提取运算符">>"相似,不同之处是可以读取包含空格的字符。

    int get();
    istream& get (char& c);	
    istream& get (char* s, streamsize n);
    istream& get (char* s, streamsize n, char delim);
    istream& get (streambuf& sb);
    istream& get (streambuf& sb, char delim);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    #include 
    #include 
    using namespace std;
    
    int main()
    {
    	char ch;
    	int n = 0;
    	ifstream ifile("C:\\VS2013Projects\\test.txt", ios::out);
    	if (!ifile)
    	{
    		cout << "test文件不能打开" << endl;
    		return 0;
    	}
    
    	while (!ifile.eof() && n < 12)
    	{
    		n++;
    		ch = ifile.get();	//从文件读取一个字符
    		cout << ch;
    	}
    	ifile.close();
    	return 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    getline用法

    #include 
    #include 
    using namespace std;
    
    int main()
    {
    	char str[12];
    	ifstream ifile("C:\\VS2013Projects\\test.txt", ios::out);
    	if (!ifile)
    	{
    		cout << "test文件不能打开" << endl;
    		return 0;
    	}
    
    	ifile.getline(str,12);
    	cout << str;
    	ifile.close();
    	return 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    read函数用法

    istream& read (char* s, streamsize n);
    
    • 1
    #include 
    #include 
    using namespace std;
    
    int main()
    {
    	char str[12];
    	ifstream ifile("C:\\VS2013Projects\\test.txt", ios::out);
    	if (!ifile)
    	{
    		cout << "test文件不能打开" << endl;
    		return 0;
    	}
    
    	ifile.read(str, 12);
    	cout << str;
    	ifile.close();
    	return 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    写文本文件的几种方法

    (1)使用流运算符"<<"

    #include 
    #include 
    using namespace std;
    
    int main()
    {
    	char str[12] = "Great Wall";
    	ofstream ofile("C:\\VS2013Projects\\test.txt", ios::in);//从头写入不覆盖
    	if (!ofile)
    	{
    		cout << "test文件不能打开" << endl;
    		return 0;
    	}
    	ofile << str;	//向文件写入
    
    	ofile.close();
    	return 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    (2)使用流成员函数
    常用的流成员函数有put和write:

    ostream& put (char c);
    
    • 1
    #include 
    #include 
    using namespace std;
    
    int main()
    {
    	char str[12] = "Great Wall";
    	char *p = str;
    
    	ofstream ofile("C:\\VS2013Projects\\test.txt", ios::in|ios::trunc);
    	if (!ofile)
    	{
    		cout << "test文件不能打开" << endl;
    		return 0;
    	}
    	while (*p!='\0')
    	{
    		ofile.put(*p); //向文件写入一个字符
    		p++;
    	}
    
    	ofile.close();
    	return 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    ostream& write (const char* s, streamsize n);
    
    • 1
    #include 
    #include 
    using namespace std;
    
    int main()
    {
    	char str[12] = "Great Wall";
    
    	ofstream ofile("C:\\VS2013Projects\\test.txt", ios::in|ios::trunc);
    	if (!ofile)
    	{
    		cout << "test文件不能打开" << endl;
    		return 0;
    	}
    	ofile.write(str, 12);//向文件写入12个字符
    
    	ofile.close();
    	return 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    二进制文件的读写

    二进制文件不同于文本文件,读写二进制的字符不需做任何转换,读写的字符与文件之间是完全一致的。二进制文件读写一般用read()/write()函数,因为他们有每次读写字符的参数。
    需要注意的是,他们的第一个参数是char*,因此需要强制转换为该指针类型。

    #include 
    #include 
    using namespace std;
    const int N = 3;
    struct Date{
    	int d, m, y;
    };
    
    int main()
    {
    	Date date[N] = { { 1, 2, 2022 }, { 10, 12, 2022 }, { 12, 12, 2022 } };
    	ofstream ofile("C:\\VS2013Projects\\data.data", ios::binary);
    	if (!ofile)
    	{
    		cout << "文件不能打开" << endl;
    		return 0;
    	}
    
    	for (int i = 0; i < N; i++)
    	{
    		ofile.write((char *)&date[i], sizeof(Date));
    	}
    
    	ofile.close();
    
    	ifstream ifile("C:\\VS2013Projects\\data.data", ios::binary);
    	if (!ifile)
    	{
    		cout << "文件不能打开" << endl;
    		return 0;
    	}
    
    	Date dt;
    	for (int i = 0; i < N; i++)
    	{
    		ifile.read((char *)&dt, sizeof(Date));
    		cout << dt.d << " " << dt.m << " " << dt.y << endl;
    	}
    
    	ifile.close();
    
    	return 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    文件的随机读写

  • 相关阅读:
    【C++入门到精通】C++入门 ——搜索二叉树(二叉树进阶)
    服务端优化-连接性能,io模型,web容器
    7.5面试问答
    使用预训练的嵌入向量
    体验下,大厂在使用功能的API网关!
    让孩子更快乐的学编程,一套积木就够了,长毛象AI百变编程积木套件体验
    2024.08.07校招 实习 内推 面经
    C语言中常用的字符串处理函数(strlen、strcpy、strcat、strcmp)
    XTU-OJ 1331-密码
    6 Go的切片
  • 原文地址:https://blog.csdn.net/wokaowokaowokao12345/article/details/126140131