Mike said:
#include <stdio.h>
int main()
{
FILE *fp = fopen("file.xls", "rb");
if(fp)
{
while(getc(fp) != EOF)
;
if(!feof(fp))
puts("Error reading input");
}
else
puts("Cannot open input");
return 0;
}
Just as an illustration of what I consider clear code, consider the
following reformatting, which preserves the input (ch) and a handy
place (continue) to insert user code:
#include <stdio.h>
int main(void)
{
FILE *fp;
int ch;
if (!(fp = fopen("file.xls", "rb") puts("Cannot open input");
else {
while (EOF != (ch = getc(fp))) {
continue;
}
if (!feof(fp)) puts("Error reading input");
}
return 0;
}
although my taste would relegate the entire while loop area to a
separate function, as in:
#include <stdio.h>
int process(FILE *fp)
{
int ch;
while (EOF != (ch = getc(fp))) {
continue;
}
return feof(fp);
}
int main(void)
{
FILE *fp;
if (!(fp = fopen("file.xls", "rb") puts("Cannot open input");
else if (!process{fp) puts("Error reading input");
return 0;
}
There are now three versions above. Consider how you would modify
each one to use command line specified files, or a list of files,
or to display some subset of the file information, assuming you had
never seen the code before, and that you want to leave the code
clearly readable.