You haven't opened any files. Open a file for reading like this:
Code:
FILE *fstRO = fopen(argv[1], "r");
if (fstRO == NULL) {perror("Couldn't open"); exit -1;}
and remember to fclose the stream (fstRO) later. In this case, the file will be whatever you use as the first argument to the program when you start it. The second line is to make sure the file has actually been opened; it requires stdlib.h . Then, you want to read character-by-character thru the file and add up the tabs -- except getchar won't do this (unless you pipe fstRO into stdin) because getchar reads from stdin. So instead use getc:
Code:
while ((getc(fstRO)) != EOF) if (c == '\t') nt++;
So here's your code with corrections:
Code:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
int c, nt=0;
FILE *fstRO = fopen(argv[1], "r");
if (fstRO == NULL) {perror("Couldn't open"); exit -1;}
while ((getc(fstRO)) != EOF) if (c == '\t') nt++;
fclose(fstRO);
printf("%d tabs\n", nt);
return 0;
}
Unfortunately, there is one last problem that you'll have to test yourself: Most likely, "tabs" as a single character don't exist in your text files at all (even if they got there because you pressed "tab" somewhere sometime). They're just a number of inserted spaces. So all the output is ever likely to be is:
0 tabs
Oh well...