Like Tree1Likes
  • 1 Post By whiteflags

trouble writing a text file

This is a discussion on trouble writing a text file within the C++ Programming forums, part of the General Programming Boards category; I am writing a text file which has the number of records in it at the start and then each ...

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

    trouble writing a text file

    I am writing a text file which has the number of records in it at the start and then each record is stored at a 400 boundary - or rather should be!

    In the following snippet the number of records is being updated and recorded at the start of the file OK but the string I am writing should be at 400, 800, 1200 etc but this part is not working, when I inspect the file there is only the one string and it's position varies only a little.

    I am sure it must be an incorrect setting but I have tried all the ios::beg etc but none seem to make a difference

    Code:
            jcin>>numrecords;
            jcin.close ();
    
            cout<<endl<<endl<<endl<<"There are currently "<<numrecords<<" records in this file.";
    
            ofstream jcout;
            jcout.open(filename, ios::out);
    
            long int pos;
            pos=(numrecords+1)*400;
    
            jcout.seekp (pos);
            jcout<<"hello sailor";//will be replace by a record
            cout<<endl<<"position is ; "<<pos<<endl;
    
            jcout.seekp (0);
            jcout<<numrecords++;//this is updating correctly
    
            jcout.close ();

  2. #2
    Registered Boozer
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    1,724
    I believe that this
    Code:
    ofstream jcout;
    jcout.open(filename, ios::out);
    will essentially truncate the file. Try an fstream instead of an ofstream. I believe it defaults to in/out mode, so it won't truncate.
    Code:
        fstream jcout(filename);
    The human mind treats a new idea the way the body treats a strange protein; it rejects it. - P.B.Medawar

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,821
    I gave up long ago trying to understand what the default behavior of the objects were. You can just mask together features you want with the pipe operator. A list of features is here: ios_base::openmode - C++ Reference.
    Code:
    ofstream jcout;
    jcout.open (filename, ios::out | ios::app);
    I still think it's good to use ofstream and ifstream though, unless you want to read and write from the same stream.
    laserlight likes this.
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble Writing To File
    By NuNn in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 09:44 AM
  2. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  3. Trouble writing characters to a file.
    By themusician31 in forum C Programming
    Replies: 7
    Last Post: 08-10-2008, 05:50 AM
  4. trouble with text file reading/writing
    By sbeehre in forum C Programming
    Replies: 25
    Last Post: 08-02-2006, 09:48 PM
  5. Trouble writing to file
    By Fyodorox in forum C++ Programming
    Replies: 7
    Last Post: 05-06-2002, 06:09 PM

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