By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,813 Members | 2,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,813 IT Pros & Developers. It's quick & easy.

Problems with file reading and writing

P: n/a
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::out | 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

Jun 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ka*****@gmail.com wrote:
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::out | 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().

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 14 '07 #2

P: n/a
ka*****@gmail.com wrote:
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
Jun 14 '07 #3

P: n/a
ka*****@gmail.com wrote:
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::out | 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
Jun 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.