• C++文件操作解析及使用(读、写文件 使用文件指针)


    进行文件I/O操作的时候要使用头文件包含了很多标准库

    下面是测试代码 创建文件并往文件中写入内容 

    当文件夹中没有下面的文件时会创建,并且会覆盖原文件重新写入

    一般是创建在编译器的那个 文件夹里

    1. #include<iostream>
    2. #include<strstream>
    3. #include<fstream>
    4. #include<iomanip>
    5. using namespace std;
    6. int main() {
    7. /*
    8. ofstream ofile;
    9. cout << "create file1" << endl;
    10. ofile.open("test.txt");
    11. if (!ofile.fail())
    12. {
    13. ofile << "name1" << " ";
    14. ofile << "sex1" << " ";
    15. ofile << "age1" << " ";
    16. ofile.close();
    17. cout << "create file2" << endl;
    18. ofile.open("test2.txt");
    19. if (!ofile.fail()) {
    20. ofile << "name2" << " ";
    21. ofile << "sex2" << "";
    22. ofile << "age2" << "";
    23. ofile.close();
    24. }

    2:下面的测试代码用了ifstream和ofstream对象实现读写文件的功能

    用户手动输入5次数据 程序把它写入文件中 然后再打开

    1. #include<iostream>
    2. #include<strstream>
    3. #include<fstream>
    4. #include<iomanip>
    5. using namespace std;
    6. int main(){
    7. char buf[128];
    8. ofstream ofile("test.txt");
    9. for (int i = 0; i < 5; i++) {
    10. memset(buf, 0, 128);//输入五次数据 程序把这五次数据写入文件中
    11. cin >> buf;
    12. ofile << buf;
    13. }
    14. ofile.close();//关闭
    15. ifstream ifile("test.txt");//然后再用ifstream打开
    16. while (!ifile.eof()) {
    17. char ch;
    18. ifile.get(ch);
    19. if (!ifile.eof())
    20. cout << ch;
    21. }
    22. cout << endl;
    23. ifile.close();
    24. }

    3:对二进制文件的读写 要用 ios::binary模式 读取需要read方法 写则需要write方法

    1. #include<iostream>
    2. #include<strstream>
    3. #include<fstream>
    4. #include<iomanip>
    5. using namespace std;
    6. int main(){
    7. char buf[50];
    8. fstream file;
    9. file.open("test.dat", ios::binary | ios::out);
    10. for (int i = 0; i < 2; i++) {
    11. memset(buf, 0, 50);
    12. cin >> buf;
    13. file.write(buf, 50);//二进制写要用 write方法
    14. file << endl;
    15. }
    16. file.close();
    17. file.open("test.dat", ios::binary | ios::in);
    18. while (!file.eof())
    19. {
    20. memset(buf, 0, 50);
    21. file.read(buf, 50);//二进制读取要read方法
    22. if (file.tellg() > 0)
    23. cout << buf;
    24. }
    25. cout << endl;
    26. file.close();
    27. return 0;
    28. }

    4:文件的追加

    在写入文件时 你可以不一次性写入全部数据 而是一部分一部分写 即不会覆盖前面的内容

    测试代码如下

    1. #include<iostream>
    2. #include<fstream>
    3. using namespace std;
    4. int main() {
    5. /*
    6. ofstream ofile("test.txt", ios::app);
    7. if (!ofile.fail()) {
    8. cout << "start write" << endl;
    9. ofile << "mary ";
    10. ofile << "girl ";
    11. ofile << "24 ";
    12. }
    13. else
    14. cout << "打不开啊";
    15. }

    5:eof成员函数可以用来判断文件结尾 

    在指定位置读写文件  用到了文件指针 可以输出你要求的位置的信息

     测试代码如下

    1. #include<iostream>
    2. #include<fstream>
    3. using namespace std;
    4. int main() {
    5. ifstream ifile;
    6. char cfileselect[20];
    7. cout << "输入文件名:";
    8. cin >> cfileselect;
    9. ifile.open(cfileselect);
    10. if (!ifile)
    11. {
    12. cout << cfileselect << "打不开" << endl;
    13. return 0;
    14. }
    15. ifile.seekg(0, ios::end);
    16. int maxpos = ifile.tellg();
    17. int pos;
    18. cout << "输入查找的位置:";
    19. cin >> pos;
    20. if (pos > maxpos) {
    21. cout << "is over file lenght" << endl;
    22. }
    23. else
    24. {
    25. char ch;
    26. ifile.seekg(pos);
    27. ifile.get(ch);
    28. cout << ch << endl;
    29. }
    30. ifile.close();
    31. return 1;
    32. }

  • 相关阅读:
    数字经济发展,助力企业数字化转型升级
    【广度优先搜索】leetcode 1162. 地图分析
    java计算机毕业设计家电售后管理系统源码+mysql数据库+系统+lw文档+部署
    dreamweaver作业静态HTML网页设计 大学美食菜谱网页制作教程(web前端网页制作课作业)
    10万奖金被瓜分,快来认识这位上榜者里的“乘风破浪的姐姐”
    QueryDet项目实战应用
    基于leetcode的算法训练:Day3
    java-php-net-python-校园后勤计算机毕业设计程序
    【Linux】31个普通信号
    Mac 配置Clion Qt 调试显示变量值
  • 原文地址:https://blog.csdn.net/jiebaoshayebuhui/article/details/126651442