Thread: Input and Output concurrently

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    1

    Input and Output concurrently

    Hi,

    I would like to know if it is possible to read a file and write to the same file at the same time.
    For example, if I have

    Code:
    #include <fstream>
    // and other headers
    
    
    using namespace std;
    
    string temp_string;
    char result;
    
    
    fstream studfile;
    studfile.open("student.dat", ios::in | ios::out); // Is this correct?
    
    while (!studfile.eof() ) {
         getline ( studfile, temp_string);
         studfile << "Results: " << result << endl;
    }
    Is there any way to manipulate a .dat file while reading it?
    I've checked quite a couple of sites, but there are only solutions for binary files..

    Thanks! =)

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It should be possible. Are you having problems?
    Do you mean doing both reading & writing in a single thread, or trying to read and write at exactly the same time? If it's the latter, then you should be using some kind of mutex to serialize either reading or writing but not both.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you're reading and writing fixed length records, yes.
    If you're reading a text file and trying to make each line longer, NO!

    Oh, and read the FAQ on why your use of eof() is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Big & Little Wong Tin-Bar Jackie Chan's Avatar
    Join Date
    May 2008
    Location
    Hong Kong
    Posts
    23
    here it should be enough testing as:
    Code:
    while (studfile)
    {
        //...
    }
    but why studfile.eof() is bad ?
    and is my suggestion better ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but why studfile.eof() is bad ?
    Hello! - Read the FAQ!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Big & Little Wong Tin-Bar Jackie Chan's Avatar
    Join Date
    May 2008
    Location
    Hong Kong
    Posts
    23
    kindly can you point me to the FAQ? because the FAQ of this board only mentions that C File I/O feof() is bad. it says nothing about C++ eof().

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Bad output -- possible input buffer problems
    By jake123 in forum C Programming
    Replies: 8
    Last Post: 02-18-2008, 03:36 PM
  3. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM

Tags for this Thread