The signature of your readFile
is wrong.
void readFile(char fileName);
readFile
is function that expects a single character, not a string. *argv[i]
returns the first letter of the string pointed to by argv[i]
.
void readFile(const char *fileName)
{
FILE* file;
int c; // getc returns an int
file = fopen(fileName, "r");
// always check for errors
if(file == NULL)
return;
do {
c = getc(file);
printf("%c", c);
if (!isalpha(c)) {
printf("\n");
}
} while (c != EOF);
fclose(file); // do not forget to close your streams.
}
Note here that your do-while
-loop is wrong, because you should check first
whether c
is EOF
. If it isn't then you can print it. It should be:
while((c = getc(file)) != EOF)
{
putc(c, stdout); // equivalent to printf("%c", c)
if(!isalpha(c))
puts(""); // equivalent to printf("\n");
}
Then you can call it like this:
int main(const int argc, char *argv[])
{
int status = 0;
for (int i = 1; i < argc; i++) {
readFile(argv[i]);
}
exit(status);
}
Also note that getc
returns a int
, not a char
, so the c
variable must be
an int
.
man fgets
#include <stdio.h>
int getc(FILE *stream);
DESCRIPTION
getc()
is equivalent to fgetc()
except that it may be implemented as a macro which evaluates stream more than once.
RETURN VALUE
fgetc()
, getc()
and getchar()
return the character read as an unsigned char
cast to an int
or EOF
on end of file or error.
See also this explanation