I pasted my function below, and this is what I want it to do: If strcmp(line, info) == 0 (true), return "file.txt" line number, then printf() line -1 through line +6 from file to terminal. ie. if match is found on line 10, output should be
line 9
line 10
...
line 16
I'm sure there's something wrong with my for loop, as this function is written, it compiles and the program runs, but nothing is printed to the screen if (true).
int check_duplicates(string info)
{
FILE *fp = fopen("file.txt", "r");
char line[100];
int line-number
while (fgets(line, sizeof(line), fp))
{
if (strcmp(line, info) == 0)
{
for (int i = (line_number -1); i < (line_number + 6); i++)
{
printf("%s\n", line);
}
return 1;
}
}
return 0;
}
If someone could help me fix whatever I've done wrong, that would be awesome!
i < (i + 7)
to ever end? 0 < 7, 1 < 8, 2 < 9, etc.(*line -1)
to be a line number?*line
is the first character of the line.