18

Given:

const string inputFile = "C:\MyFile.csv";
char buffer[10000];

How do I read the chars of the file in to the above buffer? I have been looking around online but none of the answers seem to work. They all wish to call getline().

0

4 Answers 4

39

NOTE: Start with Remy Lebeau's answer. For general file reading, this answer covers the hard way to do the job; it better matched the specific needs of this specific asker, but won't necessarily meet your needs as well as the std::vector and std::istreambuf_iterator approach Remy outlines.


Most of the time they are right about getline, but when you want to grab the file as a stream of bytes, you want ifstream::read().

// Open file
std::ifstream infile("C:\\MyFile.csv"); // and since you want bytes rather than
                                        // characters, strongly consider opening the
                                        // File in binary mode with std::ios_base::binary
// Get length of file
infile.seekg(0, std::ios::end);
size_t length = infile.tellg();
infile.seekg(0, std::ios::beg);

// Don't overflow the buffer!
if (length > sizeof (buffer)) {
    length = sizeof (buffer);
}

// Read file
infile.read(buffer, length);

Docs for ifstream::seekg()

Docs for ifstream::tellg()

NOTE: seekg() and tellg() to get the size of the file falls into the category of "usually works". This is not guaranteed. tellg() only promises a number that can be used to return to a particular point. That said...

NOTE: The file was not opened in binary mode. There can be some behind-the-scenes character translations, for example the Windows newline of \r\n being converted to the \n used by C++. length can be greater than the number of characters ultimately placed in buffer.

2019 rethink

size_t chars_read;

// Read file
if (!(infile.read(buffer, sizeof(buffer)))) { // Read up to the size of the buffer
    if (!infile.eof()) { // End of file is an expected condition here and not worth 
                         // clearing. What else are you going to read?
                         // Something went wrong while reading. Find out what and handle.
    }
}

chars_read = infile.gcount(); // Get amount of characters really read.
  • If you're looping on buffered reads until you consume the whole file, you'll want some extra smarts to catch that.

  • If you want to read the whole file in one shot, and can afford to use resizable buffers, take the advice in Remy Lebeau's answer.

2
  • 1
    According to the doc, I think you should use ifstream::end and ifstream::beg instead of ifs::end or ifs::beg.
    – tjysdsg
    Oct 6, 2019 at 9:29
  • 1
    @tjysdsg Won't matter here but advice worth following. The advantage you get from using the static member, especially going all the way back to ios is you can change the identifier for and type of the stream and not have to refactor as much. Zero-cost general code almost always better than specific code. Few other issues I need to clean up as well. Oct 6, 2019 at 17:03
23

Another option would be to use a std::vector for the buffer, then use a std::istreambuf_iterator to read from an std::ifstream directly into the std::vector, eg:

const std::string inputFile = "C:\\MyFile.csv";
std::ifstream infile(inputFile, std::ios_base::binary);

std::vector<char> buffer( std::istreambuf_iterator<char>(infile),
                          std::istreambuf_iterator<char>() );

Alternatively:

const std::string inputFile = "C:\\MyFile.csv";
std::ifstream inFile(inputFile, std::ios_base::binary);

inFile.seekg(0, std::ios_base::end);
size_t length = inFile.tellg();
inFile.seekg(0, std::ios_base::beg);

std::vector<char> buffer;
buffer.reserve(length);
std::copy( std::istreambuf_iterator<char>(inFile),
           std::istreambuf_iterator<char>(),
           std::back_inserter(buffer) );

If you go with @user4581301's solution, I would still suggest using std::vector for the buffer, at least:

//open file
std::ifstream infile("C:\\MyFile.csv");
std::vector<char> buffer;

//get length of file
infile.seekg(0, infile.end);
size_t length = infile.tellg();
infile.seekg(0, infile.beg);

//read file
if (length > 0) {
    buffer.resize(length);    
    infile.read(&buffer[0], length);
}
7

If you're concerned with efficiency (you rejected getline()) then a C-style mmap is probably best:

#include <sys/stat.h>
#include <sys/mman.h>

struct stat s;
stat(inputFile.c_str(), &s);
size_t file_size = st.st_size;

int fhand = open(inputFile);
char* file_buf = (char*)mmap(0, file_size, PROT_READ, MAP_FILE|MAP_PRIVATE, fhand, 0);
...
munmap(file_buf, file_size);
1
  • 2
    Or, on Windows, which has no mmap(), you can use CreateFile(), CreateFileMapping(), and MapViewOfFile() to accomplish the same thing. Feb 22, 2022 at 20:19
0

Example:

#include <fstream>
#include <memory>

int main()
{
    std::unique_ptr<char[]> buffer{};

    {
        std::ifstream ifs("C:\\myfile.txt", std::ios::binary); // Open the file in binary mode (It will be closed automatically)

        ifs.seekg(0, std::ios::end);
        size_t length_of_the_file = ifs.tellg();
        ifs.seekg(0, std::ios::beg);

        buffer = std::make_unique<char[]>(length_of_the_file); // Allocate the buffer (It will be freed automatically)

        ifs.read(buffer.get(), length_of_the_file);
    }

    // Do something with the buffer
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.