a.txt 如下:

ABC
87888
584587
232323
ABC
78787
12121
898989
ABC
45454
454547
898989


程式碼如下:

[root@walter yahoo-code]# cat test0517.c
#include <stdio.h>
#include <string.h>

int main()
{
    FILE *file;
    int i;
    char tmp[20] = "\0";
    char *str = "ABC";

    file = fopen("a.txt", "r");
    if (file == NULL)
        printf("Open file fail.\n");
    else
    {
        while ((fgets(tmp, sizeof tmp, file)) != NULL)
        {
            if (strcmp(tmp, str))
                for (i = 0; i < 3; i++)
                    fgets(tmp, sizeof tmp, file);
            printf("tmp = %s", tmp);
        }
    }
    fclose(file);

    return 0;
}
執行結果如下:

[root@walter yahoo-code]# gcc test0517.c
[root@walter yahoo-code]# ./a.out
tmp = 232323
tmp = 898989
tmp = 898989
[root@walter yahoo-code]#

arrow
arrow
    全站熱搜

    Walter Blyss 發表在 痞客邦 留言(0) 人氣()