Problems with file reading and writing

K

katz911

Hello,
I've encountered a strange problem which I can't seem to explain.

I've written a simple C++ class that deals with a certian binary file.
In one of the methods there, I wish to update a file - namely to read
a certain field whose offset I know, and then write to another field,
whose offset I also know.

In order to read the data, I first use file.seekg(offset, ios::beg),
and then a read command; I've checked the variable, and this part is
executed as it should be. The problem is with the writing.
To place the head where I want it, I use a file.seekp(offset,
ios::beg) statement. After this statement, regardless of what the
value of 'offset' is, file.tellp() always returns '-1'.

The file was opened using the statement:
file.open(FileName.c_str(), ios::in | ios::eek:ut | ios::binary)
where FileName is a string. The reading goes smoothly and returns the
correct values, so I doubt there's a problem with the actual opening
of the file.

Thanks in advance to whoever can solve the mystery!
GK
 
M

Marcus Kwok

I've written a simple C++ class that deals with a certian binary file.
In one of the methods there, I wish to update a file - namely to read
a certain field whose offset I know, and then write to another field,
whose offset I also know.

In order to read the data, I first use file.seekg(offset, ios::beg),
and then a read command; I've checked the variable, and this part is
executed as it should be. The problem is with the writing.
To place the head where I want it, I use a file.seekp(offset,
ios::beg) statement. After this statement, regardless of what the
value of 'offset' is, file.tellp() always returns '-1'.

The file was opened using the statement:
file.open(FileName.c_str(), ios::in | ios::eek:ut | ios::binary)
where FileName is a string. The reading goes smoothly and returns the
correct values, so I doubt there's a problem with the actual opening
of the file.

I don't know if it will help, but try doing file.clear() after reading
but before seekp().
 
D

Default User

Hello,
I've encountered a strange problem which I can't seem to explain.

I've written a simple C++ class that deals with a certian binary file.
In one of the methods there, I wish to update a file - namely to read
a certain field whose offset I know, and then write to another field,
whose offset I also know.


Rather than describe your program, show it to us. If it's long, create
a complete, minimal version that still demonstrates the problem.





Brian
 
J

John Harrison

Hello,
I've encountered a strange problem which I can't seem to explain.

I've written a simple C++ class that deals with a certian binary file.
In one of the methods there, I wish to update a file - namely to read
a certain field whose offset I know, and then write to another field,
whose offset I also know.

In order to read the data, I first use file.seekg(offset, ios::beg),
and then a read command; I've checked the variable, and this part is
executed as it should be. The problem is with the writing.
To place the head where I want it, I use a file.seekp(offset,
ios::beg) statement. After this statement, regardless of what the
value of 'offset' is, file.tellp() always returns '-1'.

The file was opened using the statement:
file.open(FileName.c_str(), ios::in | ios::eek:ut | ios::binary)
where FileName is a string. The reading goes smoothly and returns the
correct values, so I doubt there's a problem with the actual opening
of the file.

Thanks in advance to whoever can solve the mystery!
GK

Post the code. It's hard to solve problems from a description. There's a
million things you could be doing wrong which your explanation just
glosses over (only a slight exageration). Post the code.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top