I might do it like this.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main() {
FILE *fin = fopen("1.txt", "r");
FILE *fout = stdout; // for testing
char line[1000];
bool print_line = false;
while (fgets(line, sizeof line, fin) != NULL) {
if (print_line) {
fputs(line, fout);
print_line = false;
}
if (strstr(line, "PROLIST") != NULL)
print_line = true;
}
fclose(fin);
return 0;
}
Using strstr like I did means that it counts a line with "PROLIST" anywhere in it.
We can be more specific if you want
For the second one, here's a possibility.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main() {
FILE *fin = fopen("A.txt", "r");
FILE *fout = stdout; // for testing
char line[1000], last_line[1000] = {0};
bool print_line = false;
while (fgets(line, sizeof line, fin) != NULL) {
if (print_line) {
fputs(line, fout);
print_line = false;
}
if (strstr(line, "PROLIST") != NULL) {
fputs(last_line, fout);
print_line = true;
}
strcpy(last_line, line);
}
fclose(fin);
return 0;
}