Originally posted by GravtyKlz
That was from the refrence site Hammer told you to look at.
So you would want to use:
Code:
while( (fscanf(filename, "%c", &next_char) != EOF) )
Also from the same site:
Code:
RETURN VALUES
These functions return the number of input items assigned,
which can be fewer than provided for, or even zero, in the
event of a matching failure. Zero indicates that, while
there was input available, no conversions were assigned;
typically this is due to an invalid input character, such
as an alphabetic character for a `%d' conversion. The
value EOF is returned if an input failure occurs before
any conversion such as an end-of-file occurs. If an error
or end-of-file occurs after conversion has begun, the num-
ber of conversions which were successfully completed is
returned.
I'd do it like this.
Code:
#include <stdio.h>
int main()
{
const char filename[] = "test.txt";
FILE *file = fopen(filename, "r");
if ( file != NULL )
{
char ch;
while ( fscanf(file, "%c", &ch) == 1 )
{
/* do stuff */
putchar(ch);
}
fflush(stdout);
fclose(file);
}
return 0;
}
Or this.
Code:
#include <stdio.h>
int main()
{
const char filename[] = "test.txt";
FILE *file = fopen(filename, "r");
if ( file != NULL )
{
for ( ;; )
{
int ch = fgetc(file);
if( ch == EOF )
{
break;
}
/* do stuff */
putchar(ch);
}
fflush(stdout);
fclose(file);
}
return 0;
}