Instream not working for file inputs (C++)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DossarLX ODI
    Batch Manager
    Game Manager
    FFR Simfile Author
    • Mar 2008
    • 14989

    #1

    Instream not working for file inputs (C++)

    Fission and I had quite a long talk about this and we couldn't figure out why the code was not working. It builds with no errors but when I run it I get this. I continually built it after suggested changes, but each time it was just blank when I ran it.



    The catch here is to read each line from the input.txt file and then print it out. Here's the data from the input.txt file:

    Code:
    Student: John Doe
    quiz 1: 60
    quiz 2: 75
    midterm exam: 80
    final exam: 90
    
    Student: Jane Roe
    quiz 1: 80
    quiz 2: 85
    midterm exam: 70
    final exam: 75
    
    Student: Joe Shmoe
    quiz 1: 90
    quiz 2: 85
    midterm exam: 87
    final exam: 85


    Following spoiler shows the code that should have printed out the contents above, but I got nothing.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main() {
    
        ifstream in("input.txt");
        string data; // Store string from file to display.
    
        while (getline(in, data)) {
            cout << data << endl;
            // endl is like inserting \n at the end of the cout.
            // It flushes the output stream as well.
        }
        
        return 0;
    }


    Of course later on I want to read the characters after the : symbol which I assume can be done by changing the while loop to

    while (getline(in, data, ':'))

    But right now nothing is getting stored into the data string variable.
    Originally posted by hi19hi19
    oh boy, it's STIFF, I'll stretch before I sit down at the computer so not I'm not as STIFF next time I step a file
  • justin_ator
    🥓<strong><span style="col
    • Mar 2007
    • 7648

    #2
    Re: Instream not working for file inputs (C++)

    I looked into errors with it online and maybe this would be helpful:

    Code:
    3. Lastly, and perhaps more tricky. After reading the employee id using fin>>id; there will be a newline character remaining in the input buffer.
    This causes the next getline() to retrieve an empty line, and throws everything out of sync.
    To fix this, insert after the opening brace at line 59:
     
            fin.ignore(); // ignore newline character
    Last edited by Zageron; 02-5-2014, 12:34 PM.

    Comment

    • Zageron
      Zageron E. Tazaterra
      FFR Administrator
      • Apr 2007
      • 6592

      #3
      Re: Instream not working for file inputs (C++)

      I took your code.
      The only thing I found wrong with it, was that the input.txt had to be in the correct folder, depending on how you launch it.

      If you're using cmdline, it should be right next to the executable.
      If you're using visual studio, it should be in the project directory.

      Your code works as intended.


      Tip: bool isopen = in.is_open();

      Comment

      • DossarLX ODI
        Batch Manager
        Game Manager
        FFR Simfile Author
        • Mar 2008
        • 14989

        #4
        Re: Instream not working for file inputs (C++)

        Alright, now there's another problem. Pastebin of the code: http://pastebin.com/YPB4Sgq7

        Since the text file is structured as follows:
        - Name\n
        - 4 quiz grades\n
        - Newline Gap to the next name

        There is a call to getline for the name first, then the for loop calls getline four times again for the quizzes, then getline is called after the loop to make up for that newline gap. But I'm getting garbage for output.

        Edit: Ok so adding endl to the end of the cout statements made the difference between

        Code:
        C:\computing3\streamtest\streamtest\dist\Debug\Cygwin-Windows>streamtest.exe
        85e Shmoe
        to

        Code:
        C:\computing3\streamtest\streamtest\dist\Debug\Cygwin-Windows>streamtest.exe
        John Doe
        60
        75
        80
        90
        
        Jane Roe
        80
        85
        70
        75
        
        Joe Shmoe
        90
        85
        87
        85
        Last edited by DossarLX ODI; 02-5-2014, 01:34 PM.
        Originally posted by hi19hi19
        oh boy, it's STIFF, I'll stretch before I sit down at the computer so not I'm not as STIFF next time I step a file

        Comment

        Working...