Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with file I/O

Status
Not open for further replies.

madazulu

Programmer
Aug 11, 2002
2
IN
Hi

I wanted to carry out simultaneous file i/o using a single handle. I used the fstream object to open the file with the "ios::in | ios::eek:ut" protocol. I used the seekg (), read () and write () methods to search within the file, and read and write structures to the file respectively. The problem that I am facing is that when I modify record no. 10 for example by

1) Using seekg () to reach that location within the file and
2) Using write () to update the record

the values in record no. 11 change. I find that this happens only when the total number of digits of record no. 10 are increased (changing a field of value 9 to 10 for example). I don't know why this is happening. As the data is in binary format, the appropriate amount of space should be allocated for each record (12 bytes per record in my case). No problem occurs if the number of digits are unchanged.

I'm tearing my hair out!!! Please help!!! :(
 
Madazulu,
I dont know much about C++. Let me try to answer this in C.

You said that the number of digits is increasing. This is the root cause of the problem,the record size of a record in the center is dynamically increasing.

e.g.
int a = 50;
fprintf(fp,"%d",a);
will print only 2 characters to the file whereas
int a = 500;
fprintf(fp,"%d",a);
will print 3 characters to the file.
To overcome such a problem make sure that all data is written in a full format i.e. something like
fprintf(fp,"%7d",a);

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
Thanks Amit, but that doesn't help. The entire point of using the C++ classes to read and write records is that the record size remains constant irrespective of the contents. The record is written in binary format, so an integer data field will occupy 2 bytes, whether its value is 1 or 32767.

The problem that you are talking about occurs if you use fprintf because then the data is written in ASCII format. An integer in that case would occupy a maximum of 5 spaces or 5 bytes.

I'm still having the same problem. Somebody help!

Mihir
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top