www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/09/14/08:54:38

Xref: news2.mv.net comp.os.msdos.djgpp:8605
From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Binary files and ^C chars-try again
Date: Thu, 12 Sep 1996 18:37:24 -0700
Organization: Alcyone Systems
Lines: 55
Message-ID: <3238BAD4.A45980C@alcyone.com>
References: <Pine DOT GSO DOT 3 DOT 94 DOT 960911160113 DOT 2084A-100000 AT zippy>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Samuel Vincent wrote:

> > 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[]) {

Presently, traditional-style function declarations are still supported in ANSI
C; that is, curious as it is to say, it is ANSI C compliant to use them.  (At
some point in the future, though, they may disappear, but exclusion is
unlikely to become an official part of the standard for a long time to come.)

> >     FILE *fp,*fopen();
>
> The fopen function is declared in stdio.h I believe.  1) You don't need to
> declare it further here.

Right.

> 2) You seem to have declared it incorrectly
> as taking no arguments...

In C++, yes; in C, no.  In C, a function prototype declaration with nothing
but whitespace inside the function parentheses indicates an undefined (i.e.,
unchecked) number and types of arguments.  (In a function _definition_, it
means no arguments.)

In C++, the restriction is changed to be the same as declaration a function
taking no arguments.

> 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.)

Yes.  Plus a minor nitpick -- the "character" given to fputc is an int, not a
char, so the cast is wasteful.

> That exit() should really just be:   return 0;

There's nothing wrong with using exit to terminate a program, even if you're
in main.

-- 
Erik Max Francis, &tSftDotIotE   http://www.alcyone.com/max/   max AT alcyone DOT com
San Jose, California   ICBM 37 20 07 N 121 53 38 W   R^4: the 4th R is respect
"Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_

- Raw text -


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