Not reading from file

This is a discussion on Not reading from file within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm currently having problems reading a line of data from a file. My code is as follows : Code: ...

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Not reading from file

    Hello,

    I'm currently having problems reading a line of data from a file. My code is as follows :

    Code:
    // File "systime.dat" contain the latest system time in for form time_t x = time(NULL);
    // File "primary.dat" contains the same, except done a few minutes before.
    
    
        fin.open("systime.dat");
        if(!fin) cout << "LOAD ERROR" << endl;
        else cout << "LOADED FINE" << endl;
        fin >> y;
        fin.close();
        
        fin.open("primary.dat");
        if(!fin) cout << "LOAD ERROR 2" << endl;
        else cout << "LOADED FINE 2" << endl;
        fin >> r;
        fin.close();
        
        cout << "Y = " << y << ", R = " << r;
        cin.get();

    This is my debugging function, and tells me where my problem is. My output is this :
    Code:
    Y = 1109850952, R = 0
    I should get :
    Code:
    Y= 1109850952, R = 1109850743
    I'm using the same method to access these, both r and y are time_t values... I simply don't see what's wrong. Can anybody see what's wrong ?

    Thank you,
    Korhedron

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I don't see anything wrong. Post all your code. also upload a copy of your .dat files.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You use the same file stream object at both reads. Perhaps the EOF flag has been set in the first case? Call fin.clear() inbetween or create a new file object for the second case.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Magos
    You use the same file stream object at both reads. Perhaps the EOF flag has been set in the first case? Call fin.clear() inbetween or create a new file object for the second case.

    This could happen if your first data file didn't have a newline after the value, in which case clear() would do the trick.

    You could try to track it down like this:

    Code:
    #include <iostream>
    #include <fstream>
    #include <ctime>
    
    using namespace std;
    
    // File "systime.dat" contain the latest system time in for form time_t x = time(NULL);
    //
    // File "primary.dat" contains the same, except done a few minutes before.
    int main()
    {
      ifstream fin;
    
      time_t  y, r;
    
      fin.open("systime.dat");
      if(!fin)  {
        cout << "LOAD ERROR" << endl;
      }
      else  {
        cout << "LOADED FINE" << endl;
      }
    
      fin >> y;
      if (!fin) {
        cout << "??? for systim.dat" << endl;
      }
      cout << "y = " << y << endl;
      fin.close();
      //fin.clear(); // why not clear it just for the heck of it?
      
      fin.open("primary.dat");
      if(!fin) 
        cout << "LOAD ERROR 2" << endl;
      else
        cout << "LOADED FINE 2" << endl;
      fin >> r;
      if (!fin) {
        cout << "??? for primary.dat" << endl;
      }
      cout << "r = " << r << endl;
      fin.close();
      
      cout << "Y = " << y << ", R = " << r;
      cin.get();
    
      return 0;
    }
    On my Windows XP system, uncommenting the clear() fixed the output.

    With Microsoft and Borland compilers, the outputs were ok without clear(). g++ required clear(). I personally have tried to get into the habit of doing the clear() thing anytime I reuse a file object.

    Regards,

    Dave
    Last edited by Dave Evans; 03-03-2005 at 09:24 AM.

  5. #5
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    I'd already tried using a different file stream, and that didn't work.

    However, I called clear() and I now get my working input. Thank you very much !
    Korhedron

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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