How to read a file line by line?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
mitras1
Posts: 26
Joined: Mon Jan 29, 2024 8:02 am

How to read a file line by line?

Post by mitras1 »

.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How to read a file line by line?

Post 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).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mitras1
Posts: 26
Joined: Mon Jan 29, 2024 8:02 am

Re: How to read a file line by line?

Post by mitras1 »

Thanks, I'll consider that
mitras1
Posts: 26
Joined: Mon Jan 29, 2024 8:02 am

Re: How to read a file line by line?

Post 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.
Post Reply