C file access question

Gregory M. Stelmack stelmack at screamer.csee.usf.edu
Fri Sep 14 03:39:59 AEST 1990


I've got some code I'm writing for ANSI file access, and have run into a
problem that nobody here can help me with (I've asked). What I am trying to do
is implement a free space list in a file that, when adding a record, the
code will look for an appropriate free space record. If it finds one, it
sticks the data there. If it doesn't, I want to add it on to the end of the
file.

I got this to work on my Amiga using Lattice C 5.04. But, when I uploaded it
to my school's SUN computers, it doesn't run. (SUNOS 4.0.3, cc compiler).
But, it compiles fine.

Anyway, here's the skeleton code (I will send complete code to anyone who
wishes to look further):

int error=0;
long DataLen;   /* passed to function, value checked */
FILE *db;
...
db = fopen("dd.data","rb+"); /* db is checked to make sure file opened */
...
error = fseek(db,0L,2);	/* seek end of file */
if (error==-1) return(GS_DATA_SEEK_ERROR);
error = fwrite((char *)&DataLen,sizeof(DataLen),1,db);
if (error!=1) return(GS_DATA_WRITE_ERROR);   <-- error = 0, so it returns
...

I have tried the seek both ways (EOF back, start forward), and checked all
parameters, and everything looks OK. I have called perror() before returning,
and it gives an Error 0 (no error). Nothing is written to the file. 

Any ideas from anyone who is familiar with the ANSI level 2 file functions?

Thanks in advance,
-- Greg Stelmack
-- Email: stelmack at sol.csee.usf.edu
-- USmail: USF Box 1510, Tampa, FL 33620-1510
-- Amiga: the only way to compute!



More information about the Comp.lang.c mailing list