#include <fstream>

1 fstream

2 ifstream

3 ofstream

4 seekg

5 seekp

6 tellg

7 tellp

1 fstream

打开输入输出文件流

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 void main()
 5 {
 6     std::fstream fio("F:\1.txt", std::ios::in | std::ios::out);
 7 
 8     fio << "hello" << std::endl;//写入
 9     fio << "world" << std::endl;
10     fio << "hello" << std::endl;
11     fio << "china" << std::endl;
12 
13     fio.seekg(std::ios::beg);//文件指针回到文件开头,beg,begin
14 
15     for (int i = 0; i < 4; i++)//读取
16     {
17         char str[100] = { 0 };
18         fio.getline(str, 100);
19         std::cout << str << std::endl;
20     }
21 
22     fio.close();//关闭文件
23     
24     system("pause");
25 }

2 ifstream

打开输入文件流

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 void main()
 5 {
 6     std::ifstream fin("F:\1.txt");//创建读取文件流
 7 
 8     char str[100] = { 0 };
 9 
10     fin >> str;//读取
11 
12     fin.close();//关闭文件
13 
14     std::cout << str;
15     
16     system("pause");
17 }

3 ofstream

打开输出文件流

打开文件,按行写入

std::endl换行

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 void main()
 5 {
 6     std::ofstream fout;
 7 
 8     fout.open("F:\1.txt");//打开文件,如果没有文件,将会创建新的文件
 9 
10     fout << "hello" << std::endl;//写入,std::endl换行
11     fout << "china" << std::endl;
12     fout << "hello" << std::endl;
13     fout << "world" << std::endl;
14 
15     fout.close();//关闭文件
16 
17     system("pause");
18 }

std::ios::app

打开文件以便在文件的尾部添加数据

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 void main()
 5 {
 6     std::ofstream fout("F:\1.txt", std::ios::app);//打开输出文件流
 7 
 8     fout << "hello world hello china";//写入
 9 
10     fout.close();//关闭文件
11 
12     system("pause");
13 }

复制文本

ifstream负责读取,ofstream负责写入

按照字节的方式读写二进制

文件加密解密都需要字节的方式

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 //按照字节的方式读写二进制
 5 //文件加密解密都需要字节的方式
 6 
 7 void main()
 8 {
 9     std::ifstream fin("F:\1.txt", std::ios::binary);//需要复制的文件
10     std::ofstream fout("F:\new.txt", std::ios::binary);//保存复制后的文件
11 
12     if (!fin || !fout)
13     {
14         std::cout << "文件打开失败" << std::endl;
15         return;
16     }
17 
18     std::cout << "文件复制开始" << std::endl;
19     char ch = 0;
20 
21     while (fout && fin.get(ch))//引用的方式读取到一个字符
22     {
23         fout.put(ch);//写入一个字节
24     }
25 
26     fin.close();//关闭文件
27     fout.close();//关闭文件
28 
29     std::cout << "文件复制完成" << std::endl;
30 
31     system("pause");
32 }

4 seekg

对输入流操作:seekg()

fin.seekg(9, std::ios::beg);//从开始往后9个字符

fin.seekg(-9, std::ios::end);//从尾部倒数9个字符

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 void main()//二进制随机位置读取
 5 {
 6     double db[10] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1 };
 7     
 8     std::ofstream fout("F:\1.txt", std::ios::binary);//写入文件
 9     fout.write((char *)db, 80);//写入以字节为单位,因此转换为char类型
10     fout.close();
11 
12     double *p = new double[5];
13 
14     std::ifstream fin("F:\1.txt", std::ios::binary);//读取文件
15 
16     fin.seekg(-40, std::ios::end);//指针到达尾部前40个字节
17 
18     fin.read((char *)p, 40);
19     fin.close();
20 
21     for (int i = 0; i < 5; i++)
22     {
23         std::cout << p[i] << std::endl;
24     }
25 
26     system("pause");
27 }

5 seekp

对输出流操作:seekp()

seekp(9, std::ios::beg);//确定随机位置进行读写

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 void main()
 5 {
 6     std::ofstream fout("F:\1.txt");//写入文件
 7     fout << "1234567890abcdefghijklmn";
 8     fout.close();//关闭文件
 9 
10     std::ofstream Fout("F:\1.txt", std::ios::in | std::ios::out);//写入文件
11     char str[] = "helloworld";
12 
13     Fout.seekp(9, std::ios::beg);//确定随机位置进行读写
14 
15     Fout << str;//输出到文件
16     Fout.close();//关闭文件
17 
18     std::ifstream fin("F:\1.txt");//读取文件
19     char ch;
20 
21     while (fin.get(ch))//打印
22     {
23         std::cout << ch;
24     }
25     fin.close();//关闭文件
26 
27     system("pause");
28 }

6 tellg

7 tellp

对输出流操作:tellp()

long size = Fout.tellp();//当前位置距离begin有多少个字节,到尾部可以获取文件大小

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 void main()
 5 {
 6     std::ofstream fout("F:\1.txt");//写入文件
 7     fout << "1234567890abcdefghijklmn";
 8     fout.close();//关闭文件
 9 
10     std::ofstream Fout("F:\1.txt", std::ios::in | std::ios::out);//写入文件
11     char str[] = "helloworld";
12 
13     Fout.seekp(9, std::ios::end);//确定随机位置进行读写
14     long size = Fout.tellp();//当前位置距离begin有多少个字节,到尾部可以获取文件大小
15 
16     Fout << str;//输出到文件
17     Fout.close();//关闭文件
18 
19     std::ifstream fin("F:\1.txt");//读取文件
20     char ch;
21 
22     while (fin.get(ch))//打印
23     {
24         std::cout << ch;
25     }
26     fin.close();//关闭文件
27 
28     system("pause");
29 }
原文地址:https://www.cnblogs.com/denggelin/p/5675728.html