Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Question about fopen, fput, fclose? (http://www.velocityreviews.com/forums/t315021-re-question-about-fopen-fput-fclose.html)

Peter Shaggy Haywood 08-30-2003 12:05 AM

Re: Question about fopen, fput, fclose?
 
Groovy hepcat RiRyoKu-Nightmare was jivin' on 25 Aug 2003 23:51:26
-0700 in comp.lang.c.
Question about fopen, fput, fclose?'s a cool scene! Dig it!

>The following is source from a novice program that should open a file
>and allow me to write to the file until <RETURN> is pressed but it
>does not work on my


I'm not surprised it doesn't work. It doesn't even compile. Oh, no,
wait; yes it does (on C90), but with undefined behaviour. You're
forgetting the argument for exit(), but you're also forgetting to
include stdlib.h, so you don't have a declaration of exit(), and the
compiler happily assumes that it returns int (which is wrong) and
takes no arguments (which is also wrong). You don't even need exit()
in this program. Just return instead.

>#include <stdio.h>


Add this, as it's needed for the definition of EXIT_FAILURE (and the
declaration of exit(), if you use that function):

#include <stdlib.h>

>main()


Replace the above line with this, since you want to write portable
(to C99) code:

int main(void)
>{
> FILE *fp;
> char letter;


Replace the above line with this, since you need to check for EOF:

int letter;

> puts("Type something and <PRESS ENTER> when finished to save.\n");
> if((fp = fopen("MYFILE.TEXT","w"))==NULL)
> {
> puts("Cannot open the file\n");


Replace the above line with this, because error messages belong on
the standard error stream, stderr:

fputs("Cannot open the file\n", stderr);

> exit();


Replace the above line with this, because it is more logical to
return than call exit() from main() (but even if you do call exit(),
you must pass it an argument), and EXIT_FAILURE is the standard
error/failure indicating exit value:

return EXIT_FAILURE;
> }
> do
> {
> letter=getchar();


Insert the following, as you need to check for EOF, which would
indicate an error reading the input:

if(EOF == letter)
(
fputs("Error reading from standard input.\n", stderr);
return EXIT_FAILURE;
}

> fputc(letter, fp);


Insert the following, as you need to check for an error writing the
output:

if(ferror(fp))
(
fputs("Error writing to file.\n", stderr);
return EXIT_FAILURE;
}

> }
> while(letter!='\r');


Replace the above line with this, because you need to check for a
newline character (the character your program recieves when you press
"enter" or "return"):

while('\n' != letter);

> fclose(fp);
> return(0);
>}


Now your program should compile and run as expected.
BTW, the parentheses around the return value are superfluous. return
is not a function.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?


All times are GMT. The time now is 10:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.