I/O Problems - fstream

This is a discussion on I/O Problems - fstream within the C++ Programming forums, part of the General Programming Boards category; Well, how about this. *shakes head* After playing some more I finally figured it out. The file stream was going ...

  1. #16
    mol
    mol is offline
    Registered User mol's Avatar
    Join Date
    Oct 2001
    Posts
    19
    Well, how about this.

    *shakes head*

    After playing some more I finally figured it out. The file stream was going into failstate, so just closing and reopening a file isnt going to work in this instance. So, I used the magical:

    file.clear();

    And now...it works like a charm. Amazing, the simplest things can be the hardest to figure out.

    Thanks for all your help everyone!

    Here is the code for example:

    PHP Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>

    using namespace std;

    void main()
    {
        
    int count;
        
    float num;
        
    float total;
        
    float avg;

        
    ifstream inData;
        
    ofstream outData;

        
    // lets get them all on one line
        
    inData.open("datfile1.txt");
        
    outData.open("outdata.txt");

        while(
    inData >> num)
        {
            
    outData << num << " ";
        }

        
    outData << endl;

        
    inData.clear();
        
    inData.seekg(0);

        
    count 0;
        
    avg 0;
        
    total 0;

        
    outData << fixed << showpoint
        
    << setprecision(3);
        
        while(
    inData >> num)
        {
            
    total += num;
            
    count++;
        }
        
        
    avg total count;
        
    outData << avg << endl;

        
    inData.close();

    mol
    aka version2

    life is just 1's and 0's

  2. #17
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    I'm glad you got it to work.

    Here's a post-mortem evaluation. How much truth there is in it is debateable, but in an effort to understand what is happening, it's worth the effort, at least to me.

    If close() truly isn't a member function ifstreams then calling close() could cause the stream to fail and remain unavailable until the the stream is "cleared" with clear(). Finding EOF may also send flip the fail bit, necessitating the call to clear() when end of file has been reached and before resetting the file pointer, but who knows.

    My compiler, may not be as persnickity as others and allows the call to close() without putting up a stink allowing the code to run as initially posted, whereas mol's may not let that happen.

  3. #18
    Registered User
    Join Date
    Sep 2001
    Posts
    13
    >>>If close() truly isn't a member function ifstreams then calling close() could cause the stream to fail and remain unavailable until the the stream is "cleared" with clear().

    First, close() is a member of ifstream and ofstream. fstream is derived from both ifstream and ofstream, therefore it also has the close method. If it's not in your docs, its an oversite (if you're using the MSVC docs, they don't list many of the fstream members if they've been defined in one of the base classes, which in the case of fstream, is just about all of its functions).

    Second, if you try to call a function of a class that is not really a member function of the class, your compiler should choke on it. The exception would be when you're calling a function that's implemented in a seperate .dll. There are some cases there where you could actually call a function that didn't exist.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 10:06 AM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  3. more fstream problems
    By howzer in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2006, 05:17 AM
  4. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 03:46 PM
  5. Nonblocking I/O
    By fnoyan in forum Linux Programming
    Replies: 4
    Last Post: 11-29-2005, 03:37 PM

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