Very strange file action

This is a discussion on Very strange file action within the C++ Programming forums, part of the General Programming Boards category; This snippet opens a file and inputs the number of records stored at the start; but then after that you ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    Very strange file action

    This snippet opens a file and inputs the number of records stored at the start; but then after that you will see I repeat the fstream allocation (without closing the original) and everything works exactly as it should.

    However if I take this repeat fstream statement out it then doesn't output anything to the file - this I don't understand and it just doesn't seem right - is it - can anyone explain this to me?
    Code:
    filename=filename+".txt";
    fstream jcfile ( filename);
    
    if ( !jcfile.is_open() )
    {
        cout<<endl<<endl<<endl<<"The file could not be opened.";
    }
    else
    {
        jcfile>>numrecords;
        cout<<endl<<endl<<endl<<"There are currently "<<numrecords<<" records in this file.";
        numrecords++;
    
        fstream jcfile (filename);// why is this needed
    
        jcfile.seekp (numrecords*20);
        jcfile<<"hello you sailor";
    
        jcfile.seekp (0);
        jcfile<<numrecords;
        jcfile.close ();
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Why do you think it's needed? I don't do a lot of C++ I/O, but it sounds like the stream may be entering a failed state after your initial read?
    Last edited by rags_to_riches; 07-01-2012 at 05:43 AM.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    I know it is needed, I just don't know why.

    The only way I found out it was needed was originally I used istream and ostream and opened each seperately, when I changed it to fstream so that I could read and write just to one instance that statement got left in the middle by mistake - originally with a close statement before it. I tried taking the close and reeopen stements out and it didn't work then I tried with just the open statement back in and it worked as wanted I just can't understand why.

    Even stranger, I have now experimented a little and if that statement is not there and the file has no records it does not add a record but if the file exists and has say 2 records it correctly adds a third.

    When I create the file for use I simply open a file and write the number '0' to it. I have now found that id I write a space after the '0' it works as expected without the second opening statement. Could it e that becaues the file only contains the number'0' it can't extend the file but by putting a space after iyt it can then extend the file a necessary - doesn't sound like normal behaviour but it fits with what is happening?
    Last edited by JayCee++; 07-01-2012 at 06:15 AM. Reason: further experimentation

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    Why dont you just use ifstream or ofstream objects? And open them in the correct mode you require, what you have so far looks strange. See the FAQs
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    rags_to_riches' idea that the stream may be entering a failed state may have some merit. Try:
    Code:
    filename = filename + ".txt";
    fstream jcfile(filename);
    
    if (!jcfile.is_open())
    {
        cout << endl << "\n\nThe file could not be opened.";
    }
    else
    {
        jcfile >> numrecords;
        cout << endl << "\n\nThere are currently " << numrecords << " records in this file.";
        numrecords++;
    
        jcfile.clear();
    
        jcfile.seekp(numrecords * 20);
        jcfile << "hello you sailor";
    
        jcfile.seekp(0);
        jcfile << numrecords;
        jcfile.close();
    }
    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

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Thanks laserlight.

    As usual you have come up with the right answer, it works with that statement in there - is there any way I can get a brain transplant from you?!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When does an action exist?
    By Yarin in forum General Discussions
    Replies: 18
    Last Post: 10-18-2009, 02:50 PM
  2. how do I perform an action over and over...
    By Rune Hunter in forum C# Programming
    Replies: 16
    Last Post: 02-25-2005, 11:03 AM
  3. Strange file problem.
    By tilex in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2004, 12:32 AM
  4. end of file... weird action
    By sifurat in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2004, 08:53 AM

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