Problem with ifstream

This is a discussion on Problem with ifstream within the C++ Programming forums, part of the General Programming Boards category; I made this program to read the id, grade and name of a student and to create a file with ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    16

    Problem with ifstream

    I made this program to read the id, grade and name of a student and to create a file with this information and than to open that same file and display the info on the file created, but my problem is this, i for example put in:

    Grade Id Name
    50 1 Tim
    75 2 John

    It writes the above to the file but when it displays on the screen it shows the second line twice like such

    Grade Id Name
    75 2 John
    75 2 John


    Here is what i wrote

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<iomanip>
    
    using namespace std;
    
    int main()
    {
    int id, b;
    double grade;
    string filename, name;
    ifstream III;
    ofstream OOO;
    
    cout << "Please enter name for the output file" << endl;
    cin >> filename;
    
    OOO.open(filename.c_str());
    OOO << setw(10) << "Grade" << setw(10) << "Id" << setw(10) << "Name" << endl;
    
    cout << "Enter number os students that you are going to input information" << endl;
    cin >> b;
    
    for (int f=1; f<=b; f++)
    {
    cout << "Enter grade, id and name for student number " << f << endl;
    cin >> grade >> id >> name;
    OOO << setw(10) << grade << setw(10) << id << setw(10) << name << endl;
    }
    
    OOO.close();
    
    III.open(filename.c_str());
    
    cout << setw(10) << "Grade" << setw(10) << "Id" << setw(10) << "Name" << "\n";
    
    for (int f=1; f<=b; f++)
    {
    III >> grade >> id >> name;
    cout << setw(10) << grade << setw(10) << id << setw(10) << name << "\n" ;
    }
    
    III.close();
    
    system ("pause");
    return 0;
    }
    I cant figure out whats wrong,

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    The problem is that when you read from the input stream, you only retain the last set of values entered. What you probably want to do is to open the output file stream, and every time after you read in a set of input from the user, you immediately write it to the file. Alternatively, you save the input entered into an array, and later use that array to write to file.

    Oh, and you should indent your code consistently. III and OOO are not very good names.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    im sorry, i am still learning i am a noob but i am not understand really well what you are try to say,
    when i do the ofstream it writes the data correctly in order per line but when it does the ifstream it only reads the last line of the ofstream file

    and thanks for helping me

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Oh, sorry, I misread your unindented code.

    It looks like the problem is that you print the header to the file, but did not account for it when reading back in.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    so what would i have to add or change?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    It depends. One option is to simply not write the header to file. Another option is to read in the first line from the file and discard it.

    In addition, you should perform some error checking of your reading from file, which in turn would have alerted you as to the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    i havent learned about the error checking yet and if i delete the header from the ofstream it works but it wont display like the names on the file and that was one of the requirements

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Quote Originally Posted by peste19
    i havent learned about the error checking yet and if i delete the header from the ofstream it works but it wont display like the names on the file and that was one of the requirements
    What do you mean? What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    c++

    are you referring to the code cerr or to the output on the bottom of visual studio?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    No, I am not asking what language your current code is written in
    I am asking as to what is the code itself, so we can look at what you have updated.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    Its written in c++

    i know how to do stuff i just dont know their terms lol
    Last edited by peste19; 02-25-2009 at 10:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM
  2. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21