I'm using XCode and I'm trying to open a file that gets passed as a command line argument, and output the number of lines that gets passed as a command line argument of that file, to the console in C. In XCode, my arguments are "test.rtf", and "5". My rtf looks like:
line 1 test
line 2 test
line 3 test
line 4 test
line 5 test
line 6 test
line 7 test
line 8 test
line 9 test
line 10 test
I have tried this with my rtf in the same folder as my XCode project folder, and in the Debug folder where the executable is. My code is:
#include <stdio.h>
#include <stdlib.h>
#define CORRECT_PARAMETERS 2
int main(int argc, char *argv[])
{
int x;
if (argc != CORRECT_PARAMETERS) {
printf("Wrong number of parameters inputted.");
}
else {
FILE *inFp; /*declare a file pointer */
if ((inFp = fopen(argv[0], "r") == NULL)) {
fprintf(stderr, "Can't open file");
exit(EXIT_FAILURE);
}
else {
for (x = 1; x <= argv[1]; x++) {
while ((x = fgetc(inFp)) != EOF) {
printf("%c", x);
}
}
}
fclose(inFp);
}
}
I know my code may not be correct of outputting the number of lines input at the command line, but I cannot get the beginning part working of just opening the file. What gets outputted is:
Wrong number of parameters inputted.
Thanks!