Thread: Write and Read File at the same time

  1. #1
    Registered User
    Join Date
    Aug 2011

    Question Write and Read File at the same time

    I want to read and write to text file at the same time. But I have met some problem. Here is my code:
    int main()
    	string t;
    	fstream f(file,ios::out|ios::in);
    	f>>t; //read first line of file
    	f<<"C++"<<endl; //printf C++
    	f<<"Programming"<<endl; //print Programming
    	f>>t; //Read file
    	return 0;
    My text file, for example is: hello \n afternoon \n morning
    After run, my text file is no change. and t is just hello in both two cases.

    I cannot understand why like that. Help me about this problem,please

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    You should check the file opened properly:

        fstream f(file,ios::out|ios::in);
        if (!f.good() {
    		ios::iostate state = fs.rdstate();
    		if (state & ios::badbit) cerr << "Bad fstream.\n";
    		if (state & ios::failbit) cerr << "Fstream failed.\n";
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2005

    The first reading you do (immediately after opening the stream) may fail. If it does, all subsequent operations may fail unless the error status is cleared. You therefore need to check that EVERY operation is successful.

    After writing to a file then, before reading it, it is necessary to both flush and rewind the stream. Flushing is implied by std::endl, but it doesn't hurt to do it explicitly. Rewinding may be done using something like f.seekg(0) [to rewind to the start of the file]. Note that the positioning by seekg() usually works best if the file is opened in binary mode (opening in text mode means seekg() has some implementation defined behaviours).

    It is usually easier to open an output-only file, write to it, close it, reopen the same file for reading. That separates the concerns, and reduces complexity of synchronising between read and write operations - it is much easier for a programmer to confuse him/her self by interleaving reading and writing operations on the same stream .... as you have done.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. r+ in fopen doesnt let me read and write at the same time
    By shiroaisu in forum C Programming
    Replies: 8
    Last Post: 09-04-2011, 08:33 PM
  2. Read/Write file at the same time
    By doia in forum C Programming
    Replies: 2
    Last Post: 02-25-2010, 04:18 PM
  3. How to get the last write time of a file?
    By maxorator in forum Windows Programming
    Replies: 1
    Last Post: 12-12-2005, 09:52 AM
  4. .ini file Read/Write
    By waxby in forum C Programming
    Replies: 3
    Last Post: 01-05-2003, 05:39 PM