beginner IO question

S

Steven Jones

I'm just learning c and have an io question (more "i" then "o"). I could not
find a relevant answer in the FAQ. The following code is supposed to demo
fputc, fgetc and fgets. I first open a file, write a line of text to it and
close it. Next I use two separate methods to read the text back in. The output
section works as expected (verified by opening the file in an editor).
Neither input test work however. fgetc returns EOF immedatly and buffer
remains empty after the call to fgets. What am I overlooking?

Thanks in advance





#include <stdio.h>
#include <stdlib.h>

#define SIZE 80

char fname[] = "/home/sj/iotest";
char sourcetext[] = "Why ask why, try bud dry\n";
char buffer[SIZE+1];


main()
{

FILE *fptr;


printf("\nOpening %s for writing\n", fname);
fptr = fopen( fname, "w");
int i;
for(i=0; i<strlen(sourcetext); i++)
fputc( sourcetext, fptr);
close(fptr);


printf("\nRead text using fgetc\n");
fptr = fopen( fname, "r");
do{
i = fgetc( fptr);
printf("%c", (char)i);
}while( i != EOF);
close(fptr);
printf("\n\n");


printf("\nRead text using fgets\n");
fptr = fopen( fname ,"r");
fgets( buffer, SIZE, fptr);
close(fptr);
printf("buffer = %s", buffer);
printf("\n");


}


/* **** result
[sj@KUTI demo]$ ./a.out

Opening /home/sj/iotest for writing

Read text using fgetc
\377


Read text using fgets
buffer =
********* */
 
W

winnie

Maybe you should use fclsoe not close,coz fclose is the couterpart of
fopen and close is the counterpart of open.
 
M

Malcolm

Steven Jones said:
#include <stdio.h>
#include <stdlib.h>

#define SIZE 80

char fname[] = "/home/sj/iotest";
char sourcetext[] = "Why ask why, try bud dry\n";
char buffer[SIZE+1];


main()
{

FILE *fptr;


printf("\nOpening %s for writing\n", fname);
fptr = fopen( fname, "w");

if(fptr == NULL)
fprintf(stderr, "can't open file\n");
int i;
for(i=0; i<strlen(sourcetext); i++)
fputc( sourcetext, fptr);
close(fptr);

No close, fclose(). Strictly you should also check fclose for failure,
though it is much less likely to fail than fopen (usually only if you mange
to fill the disk between the call to fopen and fclose).
printf("\nRead text using fgetc\n");
fptr = fopen( fname, "r");
/* Note that this code will print the EOF - you can avoid this by a rewrite
of logic */
do{
i = fgetc( fptr);
printf("%c", (char)i);
}while( i != EOF);
close(fptr); fclose();
printf("\n\n");


printf("\nRead text using fgets\n");
fptr = fopen( fname ,"r");
fgets( buffer, SIZE, fptr);
close(fptr);
fclose()

printf("buffer = %s", buffer);
printf("\n");


}


/* **** result
[sj@KUTI demo]$ ./a.out

Opening /home/sj/iotest for writing

Read text using fgetc
\377


Read text using fgets
buffer =
********* */
 
S

Steven Jones

Maybe you should use fclsoe not close,coz fclose is the couterpart of
fopen and close is the counterpart of open.


Wow you guys are fast! fclose did it, thanks a lot
 
K

Keith Thompson

Steven Jones said:
I'm just learning c and have an io question (more "i" then "o"). I
could not find a relevant answer in the FAQ. The following code is
supposed to demo fputc, fgetc and fgets. I first open a file, write
a line of text to it and close it. Next I use two separate methods
to read the text back in. The output section works as expected
(verified by opening the file in an editor). Neither input test
work however. fgetc returns EOF immedatly and buffer remains empty
after the call to fgets. What am I overlooking?

You need to increase the warning level on your compiler. When I
compile your code with "gcc -ansi -pedantic -W -Wall tmp.c -o tmp",
I get:

tmp.c:12: warning: return type defaults to `int'
tmp.c: In function `main':
tmp.c:19: warning: ISO C90 forbids mixed declarations and code
tmp.c:20: warning: implicit declaration of function `strlen'
tmp.c:20: warning: comparison between signed and unsigned
tmp.c:22: warning: implicit declaration of function `close'
tmp.c:43: warning: control reaches end of non-void function

Fixes:

Change "main()" to "int main(void)".

Add "include <string.h>".

Change "close" to "fclose". (If you had a #include for the header
that declares close(), you would have seen warnings about
incorrect arguments.)

Add "return 0;" at the end of main() (not required in C99, but
still good style).

C90 doesn't allow declarations to follow statements in a block; C99
does. If you want to be sure your code will compile with a C90
compiler, you need to move the declaration of i.
#include <stdio.h>
#include <stdlib.h>

#define SIZE 80

char fname[] = "/home/sj/iotest";
char sourcetext[] = "Why ask why, try bud dry\n";
char buffer[SIZE+1];


main()
{

FILE *fptr;


printf("\nOpening %s for writing\n", fname);
fptr = fopen( fname, "w");

You need to check whether the fopen() succeeded.
int i;
for(i=0; i<strlen(sourcetext); i++)
fputc( sourcetext, fptr);


This is more easily done with a single call to fputs().
close(fptr);


printf("\nRead text using fgetc\n");
fptr = fopen( fname, "r");
do{
i = fgetc( fptr);
printf("%c", (char)i);
}while( i != EOF);

The cast is unnecessary; a char value will be promoted to int anyway.
(Most casts are unnecessary.) printf() is a bit heavy for printing a
single character; fputc(), putc(), or putchar() is simpler.

You're using i both as a counter in the output loop, and to hold the
input character in the input loop. This isn't wrong, but it's
potentially confusing. I suggest declaring a second variable "c" to
hold the input characters.

Because you test the condition after printing the character, you're
going to print the EOF; this will likely result in character 255 being
printed (which *might* look like a 'y' with an umlaut). A better
way to write the loop is:

while ((c = fgetc(fptr)) != EOF) {
putchar(c);
}

close(fptr);
printf("\n\n");


printf("\nRead text using fgets\n");
fptr = fopen( fname ,"r");
fgets( buffer, SIZE, fptr);

Eventually you'll need to worry about what to do if the input line is
too big for the buffer, but that's not important for this small test
program.
close(fptr);
printf("buffer = %s", buffer);
printf("\n");


}
[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C pipe 1
Working with files 1
HELP:function at c returning (null) 4
strtok problem 16
Noob File IO question 8
calling fopen using function 9
What is wrong? I can't add... 16
Communicating between processes 0

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top