Discussion Locked

Read line in from text file using C/C++

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am new to C++ programming, so this might seem pretty trivial. I am trying to find the most simple way to read a line in from a text file and store it. I am able to open the file, but I am having trouble reading it in correctly. I would like to read the entire line in, and stop reading at the end of the line. I figure I want to put the line into a character array that will hold it. I don't think I want to store white space between characters. After reading the line in, I would do my processing, then continue reading lines in until EOF.

Thanks for any help you can provide me in doing this. By the way, if it is easier to do this using C as opposed to C++ commands, it does not matter.

5 replies

by siggih

(The c++ way)

Most simple way to do this is to open a filestream, point it to the file. Then use the getline function to read until end of line.

If you want to remove whitespace use it in conjunction with a sstream (stringstream) which has several stringmanipulation functions.

Example code available in your MSDN help or info/man (**nux).
Search for iostream, fstream and/or sstream.

If you want I can post code example. But I think reading the help will "do you more good" :)

/S

This discussion is closed to new replies.