Enter a filename, enter a number, and the program is supposed to get to that "line" and print it then exit. My plan to accomplish this was count newlines (i < lines) or until EOF and then print a line. However I can't get the program to stop before EOF, it doesn't seem to be paying attention to the i < line part. I've tried swtching || for && in the while loop assuming somehow that was forcing it to wait for the EOF, no help (since it of course should be an && by my logic). I've tried moving the i < lines into the while as an if break and still exact same results.
the file I input has the following textCode:#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main(void)
{
int i = 0, line, check;
FILE *fp;
char words[MAX+1], fname[MAX+1];
puts("Enter filename");
fgets(fname, 100, stdin);
if((fp = fopen(fname, "r")) == NULL)
{
fprintf(stdin, "Can't open %s file.\n", fname);
exit(1);
}
puts("Enter the line you wish to see");
scanf("%d", &line);
while(i < line && (check = fgetc(fp)) != EOF)
if(check == '\n')
i++;
fgets(words, 100, fp);
puts("Line contents:");
puts(words);
if(fclose(fp) != 0)
fprintf(stderr,"Error closing file\n");
return 0;
}
[Session started at 2007-08-03 12:53:56 -0400.]Code:one two three
test test test
four five six
seven eight nine
ten eleven twelve
Enter filename
testrun.txt
Enter the line you wish to see
2
Line contents:
\277`\220`\375
Prime has exited with status 0.
and it always prints the same garbage so obviously nothing new is getting read into words, no matter the number I input.