fseek doesn't work properly

Jonas Mellin jonas at his.UUCP
Fri Oct 20 04:19:23 AEST 1989


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 at 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

			    			  



More information about the Comp.lang.c mailing list