Write and Read File at the same time

This is a discussion on Write and Read File at the same time within the C++ Programming forums, part of the General Programming Boards category; I want to read and write to text file at the same time. But I have met some problem. Here ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    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:
    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
    	f.close();
    	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
    thanks

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

    Code:
        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
    Posts
    6,252
    Depends.

    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%.

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, 03: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, 08:52 AM
  4. .ini file Read/Write
    By waxby in forum C Programming
    Replies: 3
    Last Post: 01-05-2003, 04:39 PM

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