CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2008
    Posts
    4

    Trouble reading from a file

    So I have a problem that makes no sense to me. Here's what I have so far.

    // final2.cpp : main project file.


    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <process.h>
    #include <cstring>

    using namespace std;
    using namespace System;

    int main(array<System::String ^> ^args)
    {
    ifstream inFile;
    ofstream outFile;

    //from "memberdata.txt"
    string firstname[100];
    string lastname[100];
    int bookspurchased[100];
    int totalspent[100];

    //from "bookdata.txt"
    string title[100];
    string authorfirst[100];
    string authorlast[100];
    string publisher[100];
    string isbn[100];
    string year[100];
    string cost[100];
    string qty[100];

    //miscellaneous variables
    int a=0, c=0, b = 0;



    //reading data from memberdata file
    inFile.open("c:\\memberdata.txt");
    while (!inFile.eof())
    {

    inFile>>firstname[a]>>lastname[a]>>bookspurchased[a]>>totalspent[a];
    cout<<firstname[a]<<lastname[a]<<bookspurchased[a]<<totalspent[a]<<endl;
    a++;
    }
    inFile.close();

    //reading from bookdata file
    inFile.open("c:\\bookdata.txt");
    while (!inFile.eof())
    {
    inFile>>title[c]>>authorlast[c]>>authorfirst[c]>>publisher[c]>>isbn[c]>>year[c]>>cost[c]>>qty[c];
    cout<<title[c]<<authorlast[c]<<authorfirst[c]<<publisher[c]<<isbn[c]<<year[c]<<cost[c]<<qty[c]<<endl;
    c++;
    }
    inFile.close();




    return 0;
    }



    This code reads the first file, memberdata, fine. But it completely skips bookdata. What's the issue? If I put the entire bookdata while loop first, it will read that fine, and then skips memberdata! Any help would be appreciated.

  2. #2
    Join Date
    Mar 2007
    Location
    Montreal, Quebec, Canada
    Posts
    185

    Re: Trouble reading from a file

    Please use code tags, like this:
    [code]
    This is a block of text surrounded by
    Code:
     tags.
    Try putting
    Code:
    inFile.seekg(ios_base::beg);
    after your first inFile.close().

  3. #3
    Join Date
    Apr 2008
    Posts
    4

    Re: Trouble reading from a file

    Unfortunately that code didn't seem to do anything.

  4. #4
    Join Date
    Apr 2008
    Posts
    12

    Re: Trouble reading from a file

    The following program is similar to yours, except that I used getline(); and I declared my variables using char (someone advised me to use string instead).
    Code:
    # include <iostream>
    # include <fstream>
    
    using namespace std;
    
    int main()
    {
    
    const int size = 50;
    char Title[ size ];
    char Author[ size ];
    char ISBN[ size ];
    char price[ size ];
    char line[ size ];
    
    
    ofstream outfile;
    outfile.open("myfilename.txt",ios::app);
    outfile.close();
    
    ifstream infile;
    infile.open("myfilename.txt"); 
    while ( infile ) 
    { 
    
       
       // read another record 
       infile.getline( Title, size ); 
       infile.getline( Author, size ); 
       infile.getline( ISBN, size ); 
       infile.getline( price, size ); 
       infile.getline( line, size );
       
       
          
       // display    
       cout << Title  << endl 
         << Author << endl 
         << ISBN   << endl 
         << price  << endl << endl; 
    
       
    
    
    }// end while 
    
    if (infile == false) 
       cout<<"Could not open the input file"<<endl;   
    
       
       infile.close();
       
       system ("pause");
    }
    Book information from file:
    Code:
    Harry Potter and the Deathly Hallows 
    J K Rowlin and Mary GrandPré 
    0545010225 
    20.99 
    ------------------------------------
    Pride and Prejudice 
    Jane Austen 
    0307386864 
    7.95 
    ------------------------------------
    Wuthering Heights 
    Emily Bronte 
    0553212583 
    4.95
    ------------------------------------
    Last edited by 01010011; April 29th, 2008 at 02:32 PM.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Trouble reading from a file

    Once you get to EOF in the first file, that state of the stream is
    such that any use after that will fail (even if you open a
    different file).

    One solution is to use a different ifstream object for the second file.

    Another solution is to clear the inernal states of the stream object
    so that it can be used again.

    Code:
    inFile.close();
    
    in.clear();   // add this line
    
    //reading from bookdata file
    inFile.open("c:\\bookdata.txt");

  6. #6
    Join Date
    Oct 2004
    Posts
    296

    Re: Trouble reading from a file

    1) When an ifstream object hits the end of the file, a flag is set inside the object that puts it into an error state. When an ifstream object is in an error state, it cannot be used to read from a file. You can use clear() to reset the flag in the ifstream object.

    2) When you read from a file, your read statement should be the while loop conditonal. If instead your while loop conditional checks for eof, then your while loop might become an infinite loop. The way that could happen is if an error occurs while reading from the file. After an error occurs, you will no longer be able to read from the file, but you won't be at eof either, so your while loop will become an infinite loop.

    3)
    Code:
    int main(array<System::String ^> ^args)
    What in the hell is that?
    Last edited by 7stud; April 29th, 2008 at 01:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured