Reading from text File

Mar 26, 2016 at 1:18am
I have a simple question

I am trying to read from a text file, where each data is separated by a comma so for example:

int40, twoYear, 233John,
fourdouble, 1isnotID,


My task is to read in each segment like "int40" analyze this, and read the next one etc.

I've only learned how to read the whole file at once, how would I read this text file part by part and what would my loop condition be? !eof()?
Mar 26, 2016 at 4:41am
bump please need help!
Mar 26, 2016 at 11:07am
what would my loop condition be? !eof()?

Try to avoid the use of eof() as a loop condition. It causes more problems than it solves. Best avoided completely.



You can read the file a line at a time like this:
1
2
3
4
5
6
7
8
    ifstream fin("file.txt");
    
    string line;
    
    while (getline(fin, line))
    {
        cout << "line is " << line << '\n'; 
    }  

Output:
line is int40, twoYear, 233John,
line is fourdouble, 1isnotID,

Now the default delimiter to indicate the end of a line is the newline '\n'. We can change that to something more appropriate, a comma in this case.
1
2
3
4
5
6
    const char comma = ',';
    
    while (getline(fin, line, comma))
    {
        cout << "line is " << line << '\n'; 
    }

Output:
line is int40
line is  twoYear
line is  233John
line is
fourdouble
line is  1isnotID

There's something unsatisfactory about that. The string line is including some whitespace. The leading whitespace can be eliminated like this:
1
2
3
4
    while (fin >> ws, getline(fin, line, comma))
    {
        cout << "line is " << line << '\n'; 
    }

Output:
line is int40
line is twoYear
line is 233John
line is fourdouble
line is 1isnotID

Last edited on Mar 26, 2016 at 11:09am
Mar 26, 2016 at 8:18pm
My friend... you are a god amongst men, thank you so much for all this info!
Mar 26, 2016 at 11:10pm
Can you explain what fin >> ws does? I'm assuming ws stands for white space, so is this saying, all white spaces are basically being "buffered" into fin?
Mar 26, 2016 at 11:27pm
http://www.cplusplus.com/reference/istream/ws/

cplusplus_com wrote:
Extract whitespaces

Extracts as many whitespace characters as possible from the current position in the input sequence. The extraction stops as soon as a non-whitespace character is found. These extracted whitespace characters are discarded.
Mar 26, 2016 at 11:46pm
Ah ty once again.

I have another problem

So my text file contains:
int Bob, 2you, john, AdamSmith;
float taxYear=2013, taxRate=29.2.3;

and instead of breaking them up into segments depending on commas, I need to do it with spaces. So I need to read "int" and "Bob" as separate strings. I did this by replacing your code where you have "," with just " " which works fine BUT:

When I try to read it seperate by a space, it will read Bob, instead of Bob. How can I also ignore the comma? Or would I have to remove the commas first?
Mar 27, 2016 at 5:19am
As you give more details of the problem, the solution begins to get clearer.
My advice is to break down the problem into smaller pieces. In this case, I would leave the code I suggested yesterday as it is.

What I would do is take the string such as "int Bob" or "float taxYear=2013" and consider that on its own. (It might be a good idea to use a separate function for handling this). Imagine you had a file which contained only "int Bob", think of the problem in that way.

Now you can do exactly that, using a stringstream. This allows you to use a string as though it was an input file (or as an output file).

For example,
1
2
3
4
5
6
7
    string input = "int Bob";
    istringstream ss(input);
    string word;
    while (ss >> word)
    {
        cout << "word is: " << word << '\n'; 
    }

Of course, instead of supplying the value for input as I've done here, you'd use the segment read from your file.
See the example in the reference pages:
http://www.cplusplus.com/reference/sstream/istringstream/istringstream/

It's possible that this is a false start and my advice is complete rubbish here. Because I don't have a full view of the problem, and where it is heading, this approach may be unsuitable.

Last edited on Mar 27, 2016 at 5:22am
Topic archived. No new replies allowed.