-1

I'm attempting to write a lexer and parser but I'm having trouble getting the final variable in a text file due to in_file.tellg() equaling -1. My program only works if I add a space character after the variable, otherwise I get a compiler error. I want to mention that I'm able to get every other variable in the text file but the last one. I believe the cause of the problem is in_file.peek()!=EOF setting in_file.tellg() to -1.

My program is something like this:

ifstream in(file_name);
char c;
in >> noskipws;

while(in >> c ){
     if(is_letter_part_of_variable(c)) {
          int start_pos = in.tellg(),
              end_pos,
              length;
          while(is_letter_part_of_variable(c) && in.peek()!=EOF ) {
              in>>c;
          }
          end_pos = in.tellg(); // This becomes -1 for some reason
          length = end_pos - start_pos; // Should be 7

          // Reset file pointer to original position to chomp word.
          in.clear();
          in.seekg(start_pos-1, in.beg);

          // The word 'message' should go in here.
          char *identifier = new char[length];
          in.read(identifier, length);
          identifier[length] = '\0'; 
     }



}

example.text

message = "Hello, World"
print message

I tried removing peek()!= EOF which gives me an eternal loop. I tried !in_file.eof() and that also makes tellg() equal to -1. What can I do to fix/enhance this code?

Amjad
  • 60
  • 6
  • @πάνταῥεῖ Does not answer my question as I'm reading a file character by character. Also, I am using the ```>>``` operator. – Amjad Apr 14 '19 at 17:11

1 Answers1

1

I believe the cause of the problem is in_file.peek()!=EOF setting in_file.tellg() to -1.

Close. peek attempts to read a character and returns EOF if it reads past the end of the stream. Reading past the end of a stream sets the stream's fail bit. tellg returns -1 if the fail bit is set.

Simple Solution

clear the fail bit before calling tellg.

Better solution

Use std::string.

std::string identifier;
while(in>>c && is_letter_part_of_variable(c)) {
    identifier += c;
}

All of the messing around with peek, seekg, tellg and the dreaded new vanish.

user4581301
  • 29,019
  • 5
  • 26
  • 45