lseek problem

jsmng at sysmgr.cs.uh.edu jsmng at sysmgr.cs.uh.edu
Sun Oct 8 19:08:29 AEST 1989


I have a small problem about lseek. I
tried to use the following program to
lseek a file ( with numbers 1 2 3 ).
I expect to get back 1, blank, and 1 again.
But my output was 1, blank and 2.
Initially I use fscanf(fp,"%f",&float_variable)
in place of fgetc and received 1 2 3 instead
of 1 2 1. Can anyone tell me what I did wrong?
Thanks. I think this is probably something
simple so please just send me a mail and
I will summarize it.


#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h> 
main(argc,argv)
int argc;
char *argv[];
{
  FILE *fp;
  char c1,c2,c3;

  if ((fp = fopen(argv[1],"r")) == NULL) {
    perror("cannot open file");
    exit(1);
  }
  c1 = fgetc(fp);
  printf("first number = %c\n",c1);
  c2 = fgetc(fp);
  printf("second number = %c\n",c2);
  if (lseek(fp,0,L_SET) == NULL) {
    perror("lseek error");
    exit(1);
  }
  c3 = fgetc(fp);
  printf("first number again = %c\n",c3);
}



More information about the Comp.lang.c mailing list