How to read a file in multiple chunks until EOF (C++)

前端 未结 3 1525
失恋的感觉
失恋的感觉 2021-02-20 15:32

So, here\'s my problem: I want to make a program that reads chunks of data from a file. Let\'s say, 1024 bytes per chunk. So I read the first 1024 bytes, perform various operati

3条回答
  •  庸人自扰
    2021-02-20 16:03

    You can do this with a loop:

    std::ifstream fin("C:\\file.txt", std::ifstream::binary);
    std::vector buffer (1024,0); //reads only the first 1024 bytes
    
    while(!fin.eof()) {
        fin.read(buffer.data(), buffer.size())
        std::streamsize s=fin.gcount();
        ///do with buffer
    }
    

    ##EDITED

    http://en.cppreference.com/w/cpp/io/basic_istream/read

提交回复
热议问题