jonas@his.UUCP (Jonas Mellin) (10/20/89)
We have a couple of HP9000/8XX machines with HPUX 3.1.
The problem:
fseek moves the filepointer to beginning of the file
regardless of the parameters
Solution:
if setbuf is used to either:
1) set no buffer
or
2) set user declared buffer
on the file;
fseek will work properly in forward searches, but it will
not work in relative searches nor in searches
from the end of the file.
Question:
Does anyone else have this problem?
Here is a testprogram which works if the comments are removed from the
line containing the call to 'setbuf'. The program creates the file
'test' and then is then meant to update the file.
#include <stdio.h>
#define N
struct dummy_struct {
int a;
char b[N];
} a;
void main()
{
FILE *fp;
int i;
char buf[BUFSIZ];
fp=fopen("test","w");
for (a.a='A'; a.a<'z'; a.a++) {
for (i=0; i<N; i++)
a.b[i]=a.a;
fwrite(&a,sizeof(a),1,fp);
}
fclose(fp);
fp=fopen("test","r+");
/* Remove comments on next line to get the program working */
/*setbuf(fp,(char *)buf); */
rewind(fp);
fread(&a,sizeof(a),1,fp);
i=0;
while (!feof(fp)) {
printf("%d %c\n",i,a.b[0]);
a.a+=2;
fseek(fp,(long)i*sizeof(a),0);
/* fseek(fp,(long)-sizeof(a),1); */
fwrite(&a,sizeof(a),1,fp);
fread(&a,sizeof(a),1,fp);
i++;
}
fclose(fp);
}
Disclaimer: Even a blind man may know how to C.
_____________________________________________
/ / / / /\ / / /| /| |E-mail: jonas@his.se
/ /--/ / \ / / / |/ | |UUCP: ...!sunic!his!jonas
/ / / / \/ / / / / | |Phone: +46 500 77646
/______________________/ \_/ onas / |ellin|Fax: +46 500 16325
Snailmail: Jonas Mellin,Hogskolan i Skovde, Box 408, 541 28 Skovde, Sweden
cpcahil@virtech.UUCP (Conor P. Cahill) (10/20/89)
In article <496@his.UUCP>, jonas@his.UUCP (Jonas Mellin) writes: > The problem: > fseek moves the filepointer to beginning of the file > regardless of the parameters > Question: > Does anyone else have this problem? > fseek(fp,(long)i*sizeof(a),0); > /* fseek(fp,(long)-sizeof(a),1); */ > fwrite(&a,sizeof(a),1,fp); > fread(&a,sizeof(a),1,fp); The problem probably is that you are using both the fread and fwrite immediatly after each other. My trusty manual specifies (on fopen(3s)): When a file is opened for update, both input and output may be done on the resulting stream. However, output may not be directly followed by input without an intervening fseek, or rewind..... RTM. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+ -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+