1.文本文件

1.写文件

写文件步骤如下:

  1. 包含头文件
    #include
  2. 创建流对象
    ofstream ofs;
  3. 打开文件
    ofs.open(“文件路径”,打开方式);
  4. 写数据
    ofs << “写入的数据”;
  5. 关闭文件
    ofs.close();

文件打开方式:

打开方式 解释
ios::in 为读文件而打开文件
ios::out 为写文件而打开文件
ios::ate 初始位置:文件尾
ios::app 追加方式写文件
ios::trunc 如果文件存在先删除,再创建
ios::binary 二进制方式

注意: 文件打开方式可以配合使用,利用|操作符

例如:用二进制方式写文件 ios::binary | ios:: out

示例:

  1. #include <iostream>
  2. using namespace std;
  3. #include <fstream>
  4. //文本文件-写文件
  5. void test01()
  6. {
  7. //1,包含头文件 fstream
  8. //2,创建流对象-写文件-ofstream
  9. ofstream ofs;
  10. //3,指定文件打开方式
  11. ofs.open("test.txt", ios::out);
  12. //4,向文件中写内容---使用左移运算符
  13. ofs << "姓名:张三" << endl;
  14. ofs << "性别:男" << endl;
  15. ofs << "年龄:22" << endl;
  16. //5,关闭文件
  17. ofs.close();
  18. }
  19. int main()
  20. {
  21. test01();
  22. system("pause");
  23. return 0;
  24. }

总结:

  • 文件操作必须包含头文件 fstream
  • 读文件可以利用 ofstream ,或者fstream类
  • 打开文件时候需要指定操作文件的路径,以及打开方式
  • 利用<<可以向文件中写数据
  • 操作完毕,要关闭文件

2.写文件

读文件与写文件步骤相似,但是读取方式相对于比较多

读文件步骤如下:

  1. 包含头文件
    #include
  2. 创建流对象
    ifstream ifs;
  3. 打开文件并判断文件是否打开成功
    ifs.open(“文件路径”,打开方式);
  4. 读数据
    四种方式读取
  5. 关闭文件
    ifs.close();

示例:

  1. #include <iostream>
  2. using namespace std;
  3. #include <fstream>
  4. #include <string>
  5. //文本文件-读文件
  6. void test01()
  7. {
  8. //1,包含头文件 fstream
  9. //2,创建流对象-读文件-ifstream
  10. ifstream ifs;
  11. //3,指定文件打开方式--并且判断是否打开成功
  12. ifs.open("test.txt", ios::in);
  13. if (!ifs.is_open())
  14. {
  15. cout << "打开文件失败!!!" << endl;
  16. return;
  17. }
  18. //4,从文件中读数据
  19. ////第一种读取方式
  20. //char buf[1024] = { 0 };
  21. //while (ifs >> buf)
  22. //{
  23. // cout << buf << endl;
  24. //}
  25. ////第二种读取方式
  26. //char buf[1024] = { 0 };
  27. //while (ifs.getline(buf,sizeof(buf)))
  28. //{
  29. // cout << buf << endl;
  30. //}
  31. ////第三种读取方式
  32. //string buf;
  33. //while (getline(ifs,buf))
  34. //{
  35. // cout << buf << endl;
  36. //}
  37. //第四种读取方式
  38. char c;
  39. while ((c=ifs.get()!=EOF))//EOF-->end of file 文件尾部的标志
  40. {
  41. cout << c ;
  42. }
  43. //5,关闭文件
  44. ifs.close();
  45. }
  46. int main()
  47. {
  48. test01();
  49. system("pause");
  50. return 0;
  51. }

总结:

  • 读文件可以利用 ifstream ,或者fstream类
  • 利用is_open函数可以判断文件是否打开成功
  • close 关闭文件

2.二进制文件

以二进制的方式对文件进行读写操作

打开方式要指定为 ios::binary

1 写文件

二进制方式写文件主要利用流对象调用成员函数write

函数原型 :ostream& write(const char * buffer,int len);

参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数

示例:

  1. #include <iostream>
  2. using namespace std;
  3. #include <fstream>
  4. #include <string>
  5. //二进制文件 写文件
  6. class Person
  7. {
  8. public:
  9. char m_Name[64];
  10. int m_age;
  11. };
  12. void test()
  13. {
  14. //1,包含头文件 fstream
  15. //2,创建流对象-写文件-ofstream
  16. ofstream ofs("person.txt", ios::out | ios::binary);
  17. //3,指定文件打开方式
  18. //ofs.open("person.txt",ios::out|ios::binary);
  19. //4,向文件中写内容---使用左移运算符
  20. Person p = { "张三",18 };
  21. ofs.write((const char*)&p, sizeof(Person));
  22. //5,关闭文件
  23. ofs.close();
  24. }
  25. int main()
  26. {
  27. test();
  28. system("pause");
  29. return 0;
  30. }

总结:

  • 文件输出流对象 可以通过write函数,以二进制方式写数据

2 读文件

二进制方式读文件主要利用流对象调用成员函数read

函数原型:istream& read(char *buffer,int len);

参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数

  1. #include <iostream>
  2. using namespace std;
  3. #include <fstream>
  4. #include <string>
  5. //二进制文件 写文件
  6. class Person
  7. {
  8. public:
  9. char m_Name[64];
  10. int m_age;
  11. };
  12. void test()
  13. {
  14. //1,包含头文件 fstream
  15. //2,创建流对象-写文件-ifstream
  16. ifstream ifs;
  17. //3,指定文件打开方式 判断是否打开成功
  18. ifs.open("person.txt", ios::in | ios::binary);
  19. if (!ifs.is_open())
  20. {
  21. cout << "文件打开失败" << endl;
  22. return;
  23. }
  24. //4,向文件中写内容---使用左移运算符
  25. Person p;
  26. ifs.read((char*)&p, sizeof(Person));
  27. cout << "姓名:" << p.m_Name << "年龄:" << p.m_age << endl;
  28. //5,关闭文件
  29. ifs.close();
  30. }
  31. int main()
  32. {
  33. test();
  34. system("pause");
  35. return 0;
  36. }
  • 文件输入流对象 可以通过read函数,以二进制方式读数据