www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/09/11/19:29:01

Date: Wed, 11 Sep 1996 16:18:02 -0700 (PDT)
From: Samuel Vincent <svincent AT cs DOT sonoma DOT edu>
To: John Moon <moon2 AT ccfsun DOT nrl DOT navy DOT mil>
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: <Pine.GSO.3.94.960911160113.2084A-100000@zippy>
MIME-Version: 1.0

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 <stdio.h>
> #include <math.h>
> 
> #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


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019