Page 1 of 1

How to read a file line by line?

Posted: Tue Jun 18, 2024 12:40 pm
by mitras1
.

Re: How to read a file line by line?

Posted: Tue Jun 18, 2024 2:08 pm
by CuteAlien
You can do something like this:

std::ifstream file("myfile.txt");
std::string line;
while (std::getline(file, line))
{
// ... do stuff with line
}

I don't know right now how this handles the different line-ending formats though (windows, linux, mac). Guess you'll have to experiment. (edit: checked stackoverflow and it says it doesn't handle that one well: https://stackoverflow.com/questions/139 ... characters)

Irrlicht loaders generally open the whole file as binary. And then parse it with their own functions (check character by character until a line-end is reached). See for example CObjMeshFilerLoader.cpp the function COBJMeshFileLoader::copyLine (not sure if that one is good code or not).

Re: How to read a file line by line?

Posted: Tue Jun 18, 2024 2:10 pm
by mitras1
Thanks, I'll consider that

Re: How to read a file line by line?

Posted: Thu Jun 20, 2024 7:42 pm
by mitras1
Hi again. I found a way to do it through core::string().split. It automatically filters empty lines and it also is easier to implement loops using this method.