I'm reading an ascii file this way:name1|name2|name3|name4|name5|name6|name7||||||||||name8|||name9
It consists in a bunch of names separated by the '|' char, some of the slots are empty. I'm using the following code to read the list of names:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main() {
std::ifstream File;
File.open("File.txt");
if (!File.is_open())
return -1;
std::vector<std::string> Names;
char Buffer[0xff];
while (!File.getline(Buffer,16,'|').eof()) {
if (strlen(Buffer) == 0)
break;
Names.push_back(Buffer);
std::cout << strerror(File.rdstate()) << std::endl;
}
return 0;
}
It is working as it should, it reads a name each time, but for some reason if it hits the char count on the second argument of File.getline()
and does not find the delimiting char, it closes itself. This is the console output I've got after running this code on a file with a big name:
File: File.txt
Ana|Bjarne Stroustrup|Alex
Console:
No error
No such file or directory
It reads the first name on the list successfully, but when it tries to read the second name, it doesn't hit the delimiting char, and for some reason it leads to the file closing by itself. I hope someone can explain to me why does this happen.