• 文件操作(个人学习笔记黑马学习)


    C++中对文件操作需要包含头文件


    文件类型分为两种:
    1.文本文件:文件以文本的ASCII码形式存储在计算机中

    2.二进制文件:文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们

    操作文件的三大类:
    1.ofstream: 写操作
    2. ifstream:读操作
    3. fstream :读写操作

    文件打开方式:

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

    文本文件

    1、写文件

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


    2、读文件

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

    二进制文件

    1、写文件

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. class Person {
    6. public:
    7. char m_Name[64];
    8. int m_Age;
    9. };
    10. void test01() {
    11. //1、包含头文件
    12. //2、创建流对象
    13. ofstream ofs("person.txt", ios::out | ios::binary);
    14. //3、打开文件
    15. //ofs.open("person.txt", ios::out | ios::binary);
    16. //4、写文件
    17. Person p = { "张三",18 };
    18. ofs.write((const char*)&p, sizeof(Person));
    19. //5、关闭文件
    20. ofs.close();
    21. }
    22. int main() {
    23. test01();
    24. system("pause");
    25. return 0;
    26. }

    2、读文件

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. class Person {
    6. public:
    7. char m_Name[64];
    8. int m_Age;
    9. };
    10. void test01() {
    11. //1、包含头文件
    12. //2、创建流对象
    13. ifstream ifs;
    14. //3、打开文件 判断文件是否打开成功
    15. ifs.open("person.txt", ios::in | ios::binary);
    16. if (!ifs.is_open()) {
    17. cout << "文件打开失败" << endl;
    18. return;
    19. }
    20. //4、读文件
    21. Person p;
    22. ifs.read((char*)&p, sizeof(Person));
    23. cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
    24. //5、关闭文件
    25. ifs.close();
    26. }
    27. int main() {
    28. test01();
    29. system("pause");
    30. return 0;
    31. }

  • 相关阅读:
    《活着》思维导图
    APIFOX公共脚本
    深入探讨芯片制程设备:从原理到实践
    【Qt】一文总结Qt高级数据结构【栈和队列】
    聊聊Hotspot内存屏障如何禁止指令重排
    申请实用新型专利有什么优势
    零基础自学Python——8大数据类型
    亚特兰蒂斯--扫描线
    【毕业季·进击的技术er】 什么是微信小程序,带你推开小程序的大门
    protobuf 反射使用总结
  • 原文地址:https://blog.csdn.net/m0_59848857/article/details/132711081