![]() |
| | #1 |
| Registered User Join Date: Jul 2008
Posts: 1
| Input and Output concurrently 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;
}
I've checked quite a couple of sites, but there are only solutions for binary files.. Thanks! =) |
| autumngardens is offline | |
| | #2 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| 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. |
| cpjust is offline | |
| | #3 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #4 |
| Big & Little Wong Tin-Bar Join Date: May 2008 Location: Hong Kong
Posts: 23
| here it should be enough testing as: Code: while (studfile)
{
//...
}
and is my suggestion better ? |
| Jackie Chan is offline | |
| | #5 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > 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. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #6 |
| Big & Little Wong Tin-Bar 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(). |
| Jackie Chan is offline | |
| | #7 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
|
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Tags |
| input, output |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Checking array for string | Ayreon | C Programming | 87 | 03-09-2009 03:25 PM |
| Bad output -- possible input buffer problems | jake123 | C Programming | 8 | 02-18-2008 03:36 PM |
| Basic C input output program help | trevordunstan | C Programming | 2 | 01-27-2008 06:41 PM |
| Base converter libary | cdonlan | C++ Programming | 22 | 05-15-2005 01:11 AM |