image.png

  • 二进制文件 存放的是数据在内存中保存的形式
  • 文本文件 存放的是单个字符

    文本文件

    文件句柄、 << >> 操作符的使用

  • 写文件 ofstream

  • 读文件 ifstream
  • 使用 string.c_str() 传入句柄函数
  • 句柄函数使用 ios::inios::out ```cpp

    include

    include

    include

    using namespace std;

int main() { // 写文件 string filename; cout << “input filename: “; cin >> filename; // 将string转换为函数参数 ofstream f1(filename.c_str(), ios::out); f1 << “hello!” << endl; f1.close(); // 读文件 ifstream f2(filename.c_str(), ios::in); string s; f2 >> s; cout << s << endl;

  1. return 0;

}

  1. <a name="kXLGG"></a>
  2. ## 读取带空格的字符
  3. 使用`getline(file, string)` --- `getline` 不会读取回车
  4. ```cpp
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. using namespace std;
  9. int main() {
  10. string filename;
  11. cout << "input filename: ";
  12. cin >> filename;
  13. // 读文件
  14. ifstream f2(filename.c_str(), ios::in);
  15. string s;
  16. getline(f2, s);
  17. cout << s << endl;
  18. f2.close();
  19. return 0;
  20. }

f.eof() 判断文件尾

读取一个完整文本文件的内容,文件有多行内容,分行读入

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. int main() {
  6. string filename, s;
  7. cout << "input filename: ";
  8. cin >> filename;
  9. // 读文件
  10. ifstream f2(filename.c_str(), ios::in);
  11. while(!f2.eof()) {
  12. getline(f2, s);
  13. cout << s << endl;
  14. }
  15. f2.close();
  16. return 0;
  17. }

在上例中,每读取一个字符,文件指针自动向后移动。当文件指针移动到文件末尾时, f.eof() 返回真,结束读取

英汉词典例子

image.png
输入一个单词,在字典中查找

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. int main()
  6. {
  7. ifstream f("英汉词典.txt", ios::in);
  8. string line_str, word2find, word;
  9. string::size_type pos;
  10. cout << "Input a word: " << endl;
  11. cin >> word2find;
  12. while (!f.eof())
  13. {
  14. getline(f, line_str);
  15. if (line_str.find(word2find, 0) != string::npos)
  16. {
  17. pos = line_str.find(" ", 0);
  18. word = line_str.substr(0, pos);
  19. if (word2find == word)
  20. {
  21. cout << line_str << endl;
  22. break;
  23. }
  24. }
  25. }
  26. if (f.eof())
  27. cout << "Not found\n";
  28. f.close();
  29. return 0;
  30. }

二进制文件

image.png
句柄函数ios::binary
read()write() 需要强制类型转换 char*

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. int main()
  6. {
  7. string s = "Hello!";
  8. ofstream f1("f2.dat", ios::binary);
  9. f1.write((char *)s.c_str(), s.size());
  10. f1.close();
  11. ifstream f2("f2.dat", ios::binary);
  12. // seek get 寻找读指针,第二个参数是偏移量
  13. f2.seekg(0, ios::end);
  14. // tellg() 返回当前的位置,而当前就是文件尾,所以返回文件长度
  15. string::size_type n = f2.tellg();
  16. string ss(n, ' '); // 需要先给ss分配空间,不然读取出错
  17. // 现在文件指针在文件尾,什么都读不到,要把文件指针只想文件头
  18. f2.seekg(0, ios::beg);
  19. // 读出来之前需要求读取的长度
  20. f2.read((char *)ss.c_str(), n);
  21. f2.close();
  22. cout << ss << endl;
  23. return 0;
  24. }

二进制文件操作一般步骤

  • 写操作 —- 创建句柄, f.write()
  • 读操作 —- 创建句柄, f.seekg(0, ios::end) string::size_type 拿到文件的长度, f.seekg(0, ios::beg) 重新定位文件指针, f.read()

    拷贝文件

    ```cpp

    include

    include

    include

    using namespace std;

int main() { string filename1, filename2; cout << “Input filename1: “; cin >> filename1; cout << “Input filename2: “; cin >> filename2;

  1. ifstream f_read(filename1.c_str(), ios::binary);
  2. ofstream f_write(filename2.c_str(), ios::binary);
  3. string s;
  4. f_read.seekg(0, ios::end);
  5. string::size_type n = f_read.tellg();
  6. f_read.seekg(0, ios::beg);
  7. f_read.read((char*)s.c_str(), n);
  8. cout << s << endl;
  9. f_write.write(filename2.c_str(), n);
  10. f_read.close();
  11. f_write.close();
  12. return 0;

} ```