Question

For some odd reason, when my file gets too big in bytes, this error occurs where it stops reading in the correct data. It's as if the buffer stops taking in data at a certain point.

I simplified my code as much as possible to determine the problem, but I've come to realization that it isn't my code, but perhaps a buffer somewhere?


If my file is 14,034 bytes and I run this simple code:

ifstream inFile("text.txt"); //File is 14,034 bytes
char test;


while(inFile >> test) //This will stop looping at about the 768 - 769 loop
    cout << test;

If I force it to go further with a for loop, the results are the same:

ifstream inFile("text.txt"); //File is 14,034 bytes
char test;

for(int count = 0; count < 14034; ++count) //The loop will continue until the
    test = inFile.get();                   //end, but for whatever reason, 
    cout << test;                          //inFile stops at 768 - 769 again

NOW: if I use the same code, but for a smaller file(say 900 bytes), I don't seem to have that problem.

EXACT SAME CODE, JUST DIFFERENT FILE SIZE

ifstream inFile("text.txt"); //File is 900 bytes
char test;

while(inFile >> test) //This loop will continue to the 900th loop without 
    cout << test;     //problems.

EXACT SAME CODE, JUST DIFFERENT FILE SIZE

ifstream inFile("text.txt"); //File is 900 bytes
char test;

for(int count = 0; count < 900; ++count) //The loop will continue until the
    test = inFile.get();                 //end (900) without problems.
    cout << test;

I've searched for my problem all over Google, and haven't been able to find a solution. The only other thread I found with a similar problem is this one:

fopen - can't write more than 16K?

But even then, no one really understood what they were talking about - so the question was technically unanswered.

Was it helpful?

Solution

It looks like you want to read the file in binary mode, which you can do as follows:

std::ifstream file("test.txt", std::ios::binary);
char byte;
while(file.read(&byte, 1)){
    // Do something with byte
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top