0

In my project, there are multiple lines of input and I am getting a strange bug where after the first line, the ifstream cuts off characters based on how far away it is from the end of the file. (If there are 3 lines, line 1 is fine. line 2 is missing the first 2 chars from the line. Line 3 is missing 1 char from the line.)

An example of input/output:

input:

John Smith
Jane Doe
Donald Trump

output:

whole line:John Smith*
What's in the stream?:John Smith.
        Next line...
whole line:Jane Doe*
What's in the stream?:ne Doe.
        Next line...
whole line:Donald Trump*
What's in the stream?:onald Trump.

Code:

Input1 function:

void input1(ifstream& in, LinkedList &linList)
{

    string wholeLine;
    int len = in.tellg();
    while(getline(in,wholeLine)){
        in.seekg(len);
        string name;
        vector<double> xArr;
        vector<double> yArr;

        cout << "whole line:" << wholeLine << "." << endl;

        string streamCheck;
        int tempLocForCheck = in.tellg();
        getline(in,streamCheck);
        in.seekg(tempLocForCheck);
        cout << "What's in the stream?:" << streamCheck << "." << endl;

        name = getName(wholeLine);    //Call to getName function

        string throwAway;
        getline(in,throwAway);
        cout << "\t\tNext line..." << endl;

        len = in.tellg();
    }
}

getName function:

string getName(string inName){
    stringstream in(inName);

    string name = "";
    bool notNumber = true;
    while(in.peek() != '\n' && notNumber){
        string letter(1,in.peek());
        if(regex_match(letter ,regex("^[A-Za-z\\s]+$"))){
            name += in.get();
        }
        else{
            notNumber = false;
        }
    }
    return name;
}

Of course, this code has been simplified to the point where anything that doesn't touch the ifstream has been excluded. I tested the code as is and the problem still persists.

I have the throwAway at the bottom because in the actual use of the code, I will have extraneous data at the end of the line.

As you can see, it seems like the tellg() is returning a different value than what the getline() would have you expect. According to the getline() we are at the beginning of the line. However, according to the tellg() we are not at the beginning of the line.

I would really appreciate any help with this. I'm sorry there is a lot of code to read through. I did my best to slim it down. I have been fiddling with it for hours trying to fix it but haven't been able to. Thanks.

I don't know if it makes a difference, but I am required to use the MinGW 4.9.2 compiler.

rph020798
  • 13
  • 3
  • The line endings may be `\r\n` on a windows system. That could explain the difference. Inspect your input file with a hex-editor to confirm that. – πάντα ῥεῖ Oct 23 '16 at 16:15
  • @πάνταῥεῖ After inspecting the input in notepad++, it shows that the lines are ending with CRLF. How does this effect my program? – rph020798 Oct 23 '16 at 16:21
  • I don't get why you are using `tellg()` here at all? What is the purpose? – πάντα ῥεῖ Oct 23 '16 at 16:21
  • I need to have the entire ifstream for the rest of the function. so I `seekg()` after I get the whole line so I can use the ifstream to grab numbers on the rest of the line. I omitted that because the problem was still happening without the other data on the line. The actual input data will look like `John Smith -23,54.6 12,3 6,1 -23,54.6`. I need the ifstream at the beginning of the line so I can save these numbers into `xArr` and `yArr` respectively. – rph020798 Oct 23 '16 at 16:30
  • There a loads of easier ways to do this. See [here](http://stackoverflow.com/questions/23047052/why-does-reading-a-record-struct-fields-from-stdistream-fail-and-how-can-i-fi) and [here](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it) for instance. You overcomplicate your code. – πάντα ῥεῖ Oct 23 '16 at 16:33
  • @πάνταῥεῖ Thank you for the suggestions. I was unaware that you could still extract doubles from stringstreams. Your suggestions of avoiding `tellg()` and `seekg()` were great. It was off because of the line endings. After switching to a stringstream, it was a lot easier and it works without any bugs. Thank you. – rph020798 Oct 23 '16 at 17:19

0 Answers0