阅读耗时大概 1分钟

    数据类型 描述
    ofstream 写操作
    ifstream 读操作
    fstream 读写操作

    使用文件操作需要用到 fstream头文件
    文件保存访问,以文本ASCII码形式存储、或者二进制形式存储
    文件打开方式

    方式 描述
    ios::in 读文件而打开文件
    ios::out 为写文件而打开文件
    ios::ate 初始位置:文件尾
    ios::trunc 如果文件存在先删除,再创建
    ios::binary 二进制方式
    ios::cur 从流的当前位置开始定位
    ios::end 从流的末尾开始定位
    ios::beg 默认的,从流的开头开始定位

    读写文件示例

    1. using namespace std;
    2. #include <iostream>
    3. #include <fstream>
    4. class Person{
    5. public:
    6. char m_Name[64];
    7. int m_Age;
    8. };
    9. int main(){
    10. // ofstream ofs;
    11. // ofs.open("person.txt", ios::out | ios::binary);
    12. // Person p = {"张三", 18};
    13. // ofs.write((const char *)&p, sizeof(Person));
    14. // ofs.close();
    15. ifstream ifs;
    16. ifs.open("person.txt", ios::in | ios::binary);
    17. if(ifs.is_open()){
    18. cout << "打开成功" << endl;
    19. Person p;
    20. ifs.read((char *) &p, sizeof(Person));
    21. ifs.close();
    22. cout << p.m_Name << " / " << p.m_Age << endl;
    23. }
    24. return 0;
    25. }

    ios::out | ios::binary 为写文件打开文件,并以二进制方式写入
    打开文件的函数如下
    void open(const char *filename, ios::openmode mode);
    istream中有一个seekg新定位文件位置指针的成员函数 和 ostrem中有一个seekp

    1. // 定位到 fileObject 的第 n 个字节(假设是 ios::beg)
    2. fileObject.seekg( n );
    3. // 把文件的读指针从 fileObject 当前位置向后移 n 个字节
    4. fileObject.seekg( n, ios::cur );
    5. // 把文件的读指针从 fileObject 末尾往回移 n 个字节
    6. fileObject.seekg( n, ios::end );
    7. // 定位到 fileObject 的末尾
    8. fileObject.seekg( 0, ios::end );

    写两个示例差不多就入门了

    1. #include <fstream>
    2. #include <iostream>
    3. using namespace std;
    4. int main ()
    5. {
    6. char data[100];
    7. // 以写模式打开文件
    8. ofstream outfile;
    9. outfile.open("afile.dat");
    10. cout << "Writing to the file" << endl;
    11. cout << "Enter your name: ";
    12. cin.getline(data, 100); //从外部读取一行
    13. // 向文件写入用户输入的数据
    14. outfile << data << endl;
    15. cout << "Enter your age: ";
    16. cin >> data;
    17. cin.ignore(); //ignore() 函数会忽略掉之前读语句留下的多余字符
    18. // 再次向文件写入用户输入的数据
    19. outfile << data << endl;
    20. // 关闭打开的文件
    21. outfile.close();
    22. // 以读模式打开文件
    23. ifstream infile;
    24. infile.open("afile.dat");
    25. cout << "Reading from the file" << endl;
    26. infile >> data;
    27. // 在屏幕上写入数据
    28. cout << data << endl;
    29. // 再次从文件读取数据,并显示它
    30. infile >> data;
    31. cout << data << endl;
    32. // 关闭打开的文件
    33. infile.close();
    34. return 0;
    35. }
    1. #include <fstream>
    2. #include <iostream>
    3. #include <fstream>
    4. #include <string>
    5. using namespace std;
    6. int main(){
    7. //1.读文件
    8. // ofstream ofs;
    9. // ofs.open("test.txt", ios::out);
    10. // ofs << "姓名 张三" << endl;
    11. // ofs.close();
    12. //2.读文件
    13. ifstream ifs;
    14. //判断文件并判断文件是否打开成功
    15. ifs.open("test.txt", ios::in);
    16. if(ifs.is_open()){
    17. cout << "文件打开成功" << endl;
    18. //char buf[1024] = {0};
    19. //2.1
    20. // while (ifs >> buf)
    21. // {
    22. // cout << buf << endl;
    23. // }
    24. //2.2
    25. // while(ifs.getline(buf, sizeof(buf))){
    26. // cout << buf << endl;
    27. // }
    28. //2.3
    29. // string buf;
    30. // while(getline(ifs, buf)){
    31. // cout << buf << endl;
    32. // }
    33. //2.4
    34. // char c;
    35. // while((c = ifs.get()) != EOF){
    36. // cout << c ;
    37. // }
    38. // ifs.close();
    39. }
    40. return 0;
    41. }