Date: Wed, 11 Sep 1996 16:18:02 -0700 (PDT) From: Samuel Vincent To: John Moon cc: djgpp AT delorie DOT com Subject: Re: Binary files and ^C chars-try again In-Reply-To: <3237032D.258A@ccfsun.nrl.navy.mil> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII On Wed, 11 Sep 1996, John Moon wrote: > Let's try this again, > > This may be a dumb question, but > when I try to read a ^C char in binary from a From what I see in your posted code, you are not using binary mode. Remember, msdos makes a distinction. > file I get an EOF condition, no matter if I try > to read further into the file or not. Is this a > perversity of the djgpp stdlib? If so, what is the > standard work-around? I have tried getc, fgetc, > fscanf, and fread, and they all do the same thing. > Apparently, the 255 (EOF) char does the same thing so > even if I get the file size and fseek past the > offending character I don't know whether it was a > ^C or EOF. > > I am using go32 v 1.12.maint3 Just so you know, version 2 is now the official version and I don't think version 1.x is really being supported anymore... > Thanks for dispelling my ignorance, > > John Moon > > PS Here's the code-it's only a few lines > > #include > #include > > #define MAX 300 > > main(argc,argv) > int argc; > char *argv[]; > { This is very old C.. I would recommend using ANSI C as follows: int main(int argc, char *argv[]) { > FILE *fp,*fopen(); The fopen function is declared in stdio.h I believe. 1) You don't need to declare it further here. 2) You seem to have declared it incorrectly as taking no arguments... > long i=0,lala; > int c,j; > char buf[255]; > > /* this creates a test binary file 0-255 named w/ contents of argv[1] */ > > if((fp=fopen(argv[1],"w"))!=NULL) { Your call to fopen should be: fopen(argv[1], "wb") The b specifies binary mode. > for(i=0; i < 256; ++i) fputc((char)i,fp); This will put 256 bytes into the file. (I foresee possible problems with your definition of char buf[255] only being able to handle 255 characters.) > fclose(fp); > } > > /* this attempts to read it back */ > > if((fp=fopen(argv[1],"r"))!=NULL) { This one should be: fopen(argv[1], "rb") for binary mode. > fseek(fp,0L,SEEK_END); > lala=ftell(fp); > printf("\n\n\nFile Size = %ld\n",lala); > rewind(fp); > for(i=0; i < MAX; ++i) { > j=fread(buf,(size_t)1,(size_t)1,fp); Here you are continuously reading a character into the first byte position of buf[]. This is fine the way it is. If you modify it to read it into buf+i to store the whole thing in buf[], you will need to expand the size of buf[] to account for your MAX of 300. BTW, you are not checking for EOF here... So you should end up with 44 of the below printfs telling you you had an EOF... (256 characters from the code that wrote to the file are going to be fine.) > printf("%ld %d %d",i,j,buf[0]); > printf("\n"); > } > } > else fprintf(stderr,"cant open %s\n",argv[1]); > > exit(0); That exit() should really just be: return 0; > } -Sam