0

I currently have a text file that is as follows:

12 6 4 9 

It is a very simple text file since I want to just get one line working and then maybe expand to multiple lines later. Extra aside: this is for a RPN calculator I am working on.

I want to go through this text file character by character. The way I currently have it implemented is with a simple while loop:

string line;
while (!infile.eof()){
    getline(infile, line);

    if (isdigit(line[0])){
        rpn_stack.push_back(atof(line.c_str()));
    }
}

rpn_stack is a vector since I will not be using the built in stack libraries in C++. The problem I am currently having is that the output is just outputting "12". Why is this?

Is there a way that I can traverse through the file character by character instead of reading as a line? Is it breaking because it finds a white space (would that be considered the EOF)?


EDIT:

The code has been rewritten to be as the following:

string line;

while (!infile.eof()){
    getline(infile, line);

    for (int i = 0; i < line.size(); i++){
        if (isdigit(line[i])){
            rpn_stack.push_back(atof(line.c_str()));
        }
    }
}

The output is 12 5 different times, which is obviously wrong. Not only are there 4 items in the txt document, but only one of them is a 12. Can someone give some insight?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 1
    `atof` will only convert the first number. `push_back` will only push back one number. You need to loop through the string you've read to extract multiple numbers from it. – ooga May 07 '14 at 03:14
  • So would this be related to my if statement: if(isdigit(line[0])){ } ? – user3610514 May 07 '14 at 03:17

1 Answers1

1

This will read as many doubles from infile as possible (i.e. until the end of file or until it comes across a token that isn't a double), separated by whitespace.

for (double d; infile >> d;)
  rpn_stack.push_back(d);

If you need parse line-by-line, as @ooga says you will need a two-stage reader that looks something like this:

for (std::string line; getline(infile, line);) {

  std::istringstream stream{line};

  for (double d; stream >> d;)
    rpn_stack.push_back(d);
}

Bonus hint: don't use .eof()

Community
  • 1
  • 1
user657267
  • 20,568
  • 5
  • 58
  • 77
  • I think he wants to do it a line at a time. Perhaps getline and stringstream. – ooga May 07 '14 at 04:01
  • Thank you for responding so quickly! Since it will be reading from the text file as a character, will this do the translation from a char to a double? – user3610514 May 07 '14 at 04:02
  • @user3610514 I've edited the answer, the `operator>>` functions of `istream`s handle all the formatting for you, all you need to do is make sure that your tokens are separated with whitespace. – user657267 May 07 '14 at 04:04
  • @user657267 Interesting. Since this will be for a RPN calculator, would the fact that a file can have no whitespace cause issue for this particular implementation? Say for example, the file reads as 4 2+? – user3610514 May 07 '14 at 04:11
  • @user3610514 what do you want your code to do with the line "4 2+" ? – M.M May 07 '14 at 04:13
  • @MattMcNabb what I would like is for it to push_back onto the vector. Then the same for the 2 (ignoring the white space). Since 4 and + are incredibly close to one another with no white space, would this cause issues in pushing onto the vector? SO essentially, my end code after all is said and done will result in the answer of 6 because of 4 + 2. Sorry if this is not coming off clearly, pretty late in my area and I'm running on little sleep, haha. – user3610514 May 07 '14 at 04:15
  • @user3610514 since `+` is not a valid char at the end of a sequence of `char`s representing a `double`, the stream's `failbit` will be set and the loop will end. If you have only one operator at the end of each line you can `clear` the stream and extract the `char`, but seeing as this is an RPN calculator you will probably need to rethink your tokenizer so it can handle reading all kinds of tokens. [The C++ Programming Language](http://www.stroustrup.com/4th.html) has a good example in one of the early chapters. – user657267 May 07 '14 at 04:18