473,788 Members | 2,868 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with file reading and writing

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(offs et, 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(offs et,
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(FileN ame.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
3 1740
ka*****@gmail.c om 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(offs et, 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(offs et,
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(FileN ame.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
ka*****@gmail.c om 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
ka*****@gmail.c om 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(offs et, 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(offs et,
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(FileN ame.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1936
by: Sara Khalatbari | last post by:
Dear friends In a code, I'm opening a file to read. Like : lines = open(filename).readlines() & I'm never closing it. I'm not writing in that file, I just read it. Will it cause any problems if you open a file to read & never close it?
8
9857
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 | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
2
17418
by: jfixsen | last post by:
Hello! Oracle 9.2.0.4 SunOS pchi-db01 5.8 Generic_108528-19 sun4u sparc SUNW,Ultra-EnterpriseSystem = SunOS Node = pchi-db01 Release = 5.8 KernelID = Generic_108528-19 Machine = sun4u BusType = <unknown>
4
9844
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile = tmpfile(); /* write into tmpFile */ ...
1
2020
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like: mdyn"comment~""comment~"\n Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried saving a record for 6/26/2004 and 7/26/2004 and it read it in as the day, year, and number of...
2
2597
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a good while and I'm getting really frustrated now! My problem is this - my custom controls periodically disappear from my
14
2818
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given by df -k lopgod10:~/mycrfile # df -k /mnt/mvdg1/vset Filesystem 1K-blocks Used Available Use% Mounted on
2
2965
by: patrickdepinguin | last post by:
Hi, I use zlib to write data structures to a compressed file, using the gzwrite function. Afterwards I read the data back with gzread. I notice that this works well when the data written is not that much, but when there is more data to write, after a while I get data errors when reading back the data. Error in main: couldn't read stat zlib error -3: test512-20070531-18h10m02.stat.gz: data error
13
10454
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another set of integers stored in an array.It would be a great help if you could provide some code for it.I tried the function fscanf but by that I am able to read only the first integer of the text file.Please help me.
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10112
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.