C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2008, 08:28 PM   #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! =)
autumngardens is offline   Reply With Quote
Old 07-10-2008, 09:42 PM   #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   Reply With Quote
Old 07-12-2008, 12:12 AM   #3
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 07-12-2008, 12:49 AM   #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 ?
Jackie Chan is offline   Reply With Quote
Old 07-12-2008, 02:18 AM   #5
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 07-14-2008, 12:43 AM   #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().
Jackie Chan is offline   Reply With Quote
Old 07-14-2008, 10:52 AM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
__________________
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   Reply With Quote
Reply

Tags
input, output

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:31 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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