* Problem with binary file I/O
@ 1998-04-27 0:56 Larry Zimmerman
1998-04-27 2:49 ` Christopher Curtis
0 siblings, 1 reply; 6+ messages in thread
From: Larry Zimmerman @ 1998-04-27 0:56 UTC (permalink / raw)
To: axp-list
I am converting a program from FORTRAN to C that reads a set of
keyed input records from an ASCII file, and writes the data
to a binary file. The keys may appear on more than one record with
different types of data. My program stores the key and data in a
structure and writes the structure to a binary file. The program
keeps a list of known keys and their record number in the binary file.
When I use the "high-level" file I/O functions, the program fails.
When I use the "low-level" functions, the program works.
I posted this problem to a couple of comp.os.linux.? newsgroups,
but got no response - maybe no-one else has this problem, or maybe
no-one believes me, or maybe no-one knows how to fix it!
I wrote a test program to demonstrate the problem - see link below -
and have discovered the following:
The record numbers used by the program are correct.
The file position (used by fseek) is correct. I also used
ftell to verify the file position.
Even though the program reports the correct file position for writing,
it appends the data to the end of the file (?????).
I have a web page with more details and the sample program at:
http://walleye.gonefishing.sk.ca/laz/rdwr/problem.html
I would appreciate any advice or help to solve this problem.
Larry Z.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: Problem with binary file I/O
1998-04-27 0:56 Problem with binary file I/O Larry Zimmerman
@ 1998-04-27 2:49 ` Christopher Curtis
0 siblings, 0 replies; 6+ messages in thread
From: Christopher Curtis @ 1998-04-27 2:49 UTC (permalink / raw)
To: axp-list
On Sun, 26 Apr 1998, Larry Zimmerman wrote:
> When I use the "high-level" file I/O functions, the program fails.
> When I use the "low-level" functions, the program works.
[...]
> no-one believes me, or maybe no-one knows how to fix it!
This seems unlikely...
> I wrote a test program to demonstrate the problem - see link below -
> and have discovered the following:
[...]
> I have a web page with more details and the sample program at:
> http://walleye.gonefishing.sk.ca/laz/rdwr/problem.html
I looked at the .c file you have at this link and the problem seems pretty
simple. Maybe that's why nobody answered, or maybe everyone's working on
the Netscape code. Anyhow, on pages 11 and 13 (of 22 - you do the math)
you have two fopen calls, each opening the file in "a+" mode. In this
mode, fseek()s will work and you can read data from any position, but all
data written will ALWAYS be written to the end of the file. This is
'append' mode and was created specifically for logging facilities. You
want to use mode "w+" (read/write) and don't worry, it won't destroy your
ouput file like the plain old "w"-mode.
> I would appreciate any advice or help to solve this problem.
--
Christopher Curtis - http://www.ee.fit.edu/users/ccurtis
Florida Institute of Technology - System Administrator, Programmer
Melbourne, Florida USA - http://www.lp.org/
^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <ccurtis@ee.fit.edu>]
* Re: Problem with binary file I/O
[not found] <ccurtis@ee.fit.edu>
@ 1998-04-27 3:37 ` Larry Zimmerman
1998-04-27 3:43 ` Christopher Curtis
1998-04-27 4:02 ` Larry Zimmerman
1 sibling, 1 reply; 6+ messages in thread
From: Larry Zimmerman @ 1998-04-27 3:37 UTC (permalink / raw)
To: axp-list
Thank you, problem solved. I guess I have an outdated man page.
The program works with fopen( ,"a+") in the addsta function,
since it does just append to this file. I used fopen( ,"r+")
in the repsta function (updates the record), and now it works.
Larry Z.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: Problem with binary file I/O
1998-04-27 3:37 ` Larry Zimmerman
@ 1998-04-27 3:43 ` Christopher Curtis
0 siblings, 0 replies; 6+ messages in thread
From: Christopher Curtis @ 1998-04-27 3:43 UTC (permalink / raw)
To: axp-list
On Sun, 26 Apr 1998, Larry Zimmerman wrote:
> Thank you, problem solved. I guess I have an outdated man page.
> The program works with fopen( ,"a+") in the addsta function,
> since it does just append to this file. I used fopen( ,"r+")
> in the repsta function (updates the record), and now it works.
D'oh! Glad you didn't listen to me. "w+" would trunc the file. Heh.
--
Christopher Curtis - http://www.ee.fit.edu/users/ccurtis
Florida Institute of Technology - System Administrator, Programmer
Melbourne, Florida USA - http://www.lp.org/
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Problem with binary file I/O
[not found] <ccurtis@ee.fit.edu>
1998-04-27 3:37 ` Larry Zimmerman
@ 1998-04-27 4:02 ` Larry Zimmerman
1998-04-27 4:26 ` Christopher Curtis
1 sibling, 1 reply; 6+ messages in thread
From: Larry Zimmerman @ 1998-04-27 4:02 UTC (permalink / raw)
To: axp-list
Christopher Curtis wrote:
D'oh! Glad you didn't listen to me. "w+" would trunc the file. Heh.
Actually I did follow up on what you suggested, and found the
right combination that way. Thanks again.
Re: pbm - I have a netpbm-1mar1994-15.alpha.rpm on my system.
I think it came from RedHat - not sure. If it's not on their
site, I can try to copy it to my anon ftp site for you or ...?
Can't help you with nn - I use knews, but I don't think it will
do what you want.
Larry Z.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~1998-04-27 4:27 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-04-27 0:56 Problem with binary file I/O Larry Zimmerman
1998-04-27 2:49 ` Christopher Curtis
[not found] <ccurtis@ee.fit.edu>
1998-04-27 3:37 ` Larry Zimmerman
1998-04-27 3:43 ` Christopher Curtis
1998-04-27 4:02 ` Larry Zimmerman
1998-04-27 4:26 ` Christopher Curtis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox