• c++文件读写


    三个类:

    • ofstream: 写文件流
    • ifstream: 读文件流
    • fstream: 读写文件流

    基本使用

    // basic file operations
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
        ofstream myfile;
        myfile.open ("example.txt");
        myfile << "Writing this to a file.\n";
        myfile.close();
        return 0;
    }
    

    打开文件
    类对象需要跟一个实际文件关联起来,程序中打开的文件使用“流”来表示,对“流”的操作会反应到实际文件中。

    open(filename, mode);
    
    mode desc
    ios::in 输入,相对CPU来说
    ios::out 输出,相对CPU来说
    ios::binary 二进制文件
    ios::ate 文件指针起始位置放到文件末尾
    ios::app 追加
    ios::trunc 截断

    操作文本文件

    // writing on a text file
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
        ofstream myfile ("example.txt");
        if (myfile.is_open())
        {
            myfile << "This is a line.\n";
            myfile << "This is another line.\n";
            myfile.close();
        }
        else cout << "Unable to open file";
        return 0;
    }
    
    // reading a text file
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
        string line;
        ifstream myfile ("example.txt");
        if (myfile.is_open())
        {
            while ( getline (myfile,line) )
            {
            cout << line << '\n';
            }
            myfile.close();
        }
    
        else cout << "Unable to open file";
    
        return 0;
    }
    

    文件状态

    • bad(): 读写操作失败时,比如写一个没有打开的文件,或者磁盘满了
    • fail(): 除了bad()的情况,还有读写格式错误,也算fail()
    • eof(): 读到文件末尾了
    • good(): 大部分都是good(),但不跟bad()互补,good()会检查更多的flags
    • clear(): 清理状态flags

    操作流的位置

    • tellg(): tell get position, 读位置

    • tellp(): tell put position, 写位置

    • seekg(): 设置读的位置

    • seekp(): 设置写的位置

    • ios::beg: 从流的开始开始

    • ios::cur: 从流的当前位置开始

    • ios: 从流的末尾开始

    注意 tellg()这些的返回值类型: streampos begin, end;

    // obtaining file size
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
        streampos begin,end;
        ifstream myfile ("example.bin", ios::binary);
        begin = myfile.tellg();
        myfile.seekg (0, ios::end);
        end = myfile.tellg();
        myfile.close();
        cout << "size is: " << (end-begin) << " bytes.\n";
        return 0;
    }
    

    操作二进制文件
    不能使用文本文件那种 << 或者 getline() 这样的函数,需要用 write(), read()

    write ( memory_block, size );
    read ( memory_block, size );
    
    // reading an entire binary file
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
        streampos size;
        char * memblock;
    
        ifstream file ("example.bin", ios::in|ios::binary|ios::ate);
        if (file.is_open())
        {
            size = file.tellg();
            memblock = new char [size];
            file.seekg (0, ios::beg);
            file.read (memblock, size);
            file.close();
    
            cout << "the entire file content is in memory";
    
            delete[] memblock;
        }
        else cout << "Unable to open file";
        return 0;
    }
    

    内部缓存
    操作文件流时,有个内部缓存对象streambuf,作为流和实际物理文件之间的缓冲。缓存同步时机:

    • 文件close
    • 内部缓存满的时候
    • 手动刷新: flush(), endl,
    • 手动刷新: sync()

    参考:
    https://www.cplusplus.com/doc/tutorial/files/

  • 相关阅读:
    【AtCoder Regular Contest 080E】Young Maids [堆][线段树]
    【AtCoder Grand Contest 007E】Shik and Travel [Dfs][二分答案]
    【Codeforces858F】Wizard's Tour [构造]
    【AtCoder Grand Contest 001F】Wide Swap [线段树][拓扑]
    【AtCoder Grand Contest 012C】Tautonym Puzzle [构造]
    【Foreign】动态规划 [分治][DP]
    【BZOJ2683】简单题 [分治][树状数组]
    【BZOJ4237】稻草人 [分治][单调栈]
    【BZOJ3745】Norma [分治]
    【BZOJ4552】【HEOI2016】排序 [二分答案][线段树]
  • 原文地址:https://www.cnblogs.com/suntus/p/16311397.html
Copyright © 2020-2023  润新知