第五题
输出含for的行:将文本文件test.txt中所有包含字符串"for"的行输出.
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define MAXN 5000 int main(void){ char string[MAXN]; FILE *fp1; if ((fp1 = fopen("text.txt", "r")) == NULL) { printf("Open file error. "); exit(0); } fgets(string, MAXN, fp1); while (!feof(fp1)) { if (strstr(string, "for") != NULL) { printf("%s", string); } fgets(string, MAXN, fp1); } if (fclose(fp1)) { printf("Can not close the file! "); exit(0); } return 0; }