473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem mixing read and write on io fstream


I have a program which opens a fstream in binary input+output mode, creating
the file if it doesn't exists. But writing doesn't works after reading, it
must be something obvious that I am not aware of.

f.open(filename,ios::in | ios::out | ios::binary | ios::trunc)

The program flow is

1) write some data
2) read the data
3) overwrite old data //this fails

I am aware that mixing reading and writting is complicated due to buffering
and as far as I know the solution is to do a seekg or seekp when changing
between reading and writting, I am doing this between 1, 2 and 3.

In 'my' program, writting in step 3 fails. I wrote a simple program that
does just the above 3 steps with seeks and it works but in my program, it
sets the failbit.

After lots of debugging in internal fstream code I figured that an internal
flag _IOREAD gets set somehow in step 2 which indicates that the stream is
now readonly and causes subsequent writes to fail. It also seems that
_IOREAD is set anytime any read operation is done on a read+write file, is
this correct? And now how do I 'unset' this flag? I tried calling f.clear
and f.seekp but that doesnt helps.

What might be special in my program that causes this read-mode flag to
remain set while this doesn't happens in a barebone/simple test program. I
am not doing anything except f.seekg, f.get, f.read.

Sachin Garg

ps. If it matters, I am using visual studio 9.0
Aug 22 '08 #1
1 3910
Hi,

This is how I read/write to the same file (my virtual file system :-) ):

BTW I open the file as follows:

File = new fstream( Filename.c_str(), ios_base::in | ios_base::out |
ios_base::binary );

and the just seekg and seekp and read or write resp.

void VResLocationManager::Store( VResLocation& Location, const std::string&
Data )
{
unsigned long TotalSize = static_cast<unsigned long>( Data.size() ) +
sizeof( unsigned long );
if( TotalSize Location.GetSize() )
{
DeleteLocation( Location );
NewLocation( Location, TotalSize );
}

fstream *File = FileSystems[ Location.GetFileNr() ]->File;
Channel << Level4 << " Offset = " << Location.GetOffset() << " Slot Size =
" << Location.GetSize() << " Data Size = " << TotalSize << " Data checksum
= " << CheckSum( Data ) << End;

File->seekp( Location.GetOffset(), ios_base::beg );

File->write( reinterpret_cast<char*>( &TotalSize ), sizeof( TotalSize ) );
if( Data.size() )
{
File->write( &Data[ 0 ], static_cast<unsigned long>( Data.size() ) );
}

}

void VResLocationManager::Retrieve( VResLocation& Location, std::string&
Data )
{
fstream *File = FileSystems[ Location.GetFileNr() ]->File;

File->seekg( Location.GetOffset(), ios_base::beg );

unsigned long TotalSize = 0;
File->read( reinterpret_cast<char*>( &TotalSize ), sizeof( TotalSize ) );

Data.resize( TotalSize - sizeof( unsigned long ) );
if( TotalSize ) File->read( &Data[ 0 ], static_cast<unsigned long>(
Data.size() ) );

// Channel << Level4 << "Retrieved Offset = " << Location.GetOffset() <<
" Slot Size = " << Location.GetSize() << " Data Size = " << TotalSize << "
Data checksum = " << CheckSum( Data ) << End;;
}

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Sachin Garg" <sa********@c10n.infowrote in message
news:g8**********@aioe.org...
>
I have a program which opens a fstream in binary input+output mode,
creating the file if it doesn't exists. But writing doesn't works after
reading, it must be something obvious that I am not aware of.

f.open(filename,ios::in | ios::out | ios::binary | ios::trunc)

The program flow is

1) write some data
2) read the data
3) overwrite old data //this fails

I am aware that mixing reading and writting is complicated due to
buffering and as far as I know the solution is to do a seekg or seekp when
changing between reading and writting, I am doing this between 1, 2 and 3.

In 'my' program, writting in step 3 fails. I wrote a simple program that
does just the above 3 steps with seeks and it works but in my program, it
sets the failbit.

After lots of debugging in internal fstream code I figured that an
internal flag _IOREAD gets set somehow in step 2 which indicates that the
stream is now readonly and causes subsequent writes to fail. It also seems
that _IOREAD is set anytime any read operation is done on a read+write
file, is this correct? And now how do I 'unset' this flag? I tried calling
f.clear and f.seekp but that doesnt helps.

What might be special in my program that causes this read-mode flag to
remain set while this doesn't happens in a barebone/simple test program. I
am not doing anything except f.seekg, f.get, f.read.

Sachin Garg

ps. If it matters, I am using visual studio 9.0


Aug 22 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
3
by: Frédéric Manzanares | last post by:
hello, my problem: I want to habe one Class with write and read in a file. i have overloaded the operator >> and <<. class c_File { public : fstream fs;
2
by: Armando | last post by:
i write one programm in vc++ but the compiler say that i use ambiguous symbol,i don´t know why ? how can do to tell the compiler that is ok so ? i become this error message: error C2872:...
13
by: JasBascom | last post by:
int main() { char record_typec = 'c'; record_typec = toupper(record_typec); * rec->Newcrecord.record_type = record_typec; etc etc \*********************************************\
5
by: Simon Verona | last post by:
I have a pair of functions that I'm calling using remoting - called readfile and writefile. The readfile works fine when called from the client PC. The writefile method returns an error: ...
5
by: znubbe | last post by:
Hi, I hope anyone can help me with this problem. I have a field of image type in a SQL 2000 database. I'm using this code to insert a document: Dim conn Dim rs Dim oStream
1
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and...
0
by: lion | last post by:
I have in my programm three files: SCANNER.CPP,tst_scanner.cpp and scaner.h i need read and write from text file which is glopal. how i can do it? the strange things if i put all programm in one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.