用 C 读取文件的一部分

时间:2021-01-22 09:23:14

标签: c

我有一个包含几个不同部分的文件。所有部分都有开始部分和结束部分线以区分部分。

如何从第 2 部分读取行?

>start Section-1
    Some words are here.
>end Section-1

>start Section-2
    Other words are also here.
>end Section-2

使用我当前的代码,打印所有文件(除单词分隔部分之外的所有部分)。我了解问题在于,在我的 fgets 中,我正在读取文件直到 #end Section-2,并且我可能需要另一个 while 循环来从特定的开始部分读取行。但我不确定如何更改代码,使其仅输出第 2 部分内的单词。

预期输出:

Other
words
are
also
here.

我现在得到的:

Some
words 
are 
here.
Other 
words 
are 
also
here. 

我的代码:

#define MAXSTR 1000
#define END ">end Section-2\n"
#define ENDWORD ">end"
#define STRWORD ">start"
#define SECTION "Section-2"

int main () {

    FILE *file; 
    char lines[MAXSTR];
    char delim[2] = " ";
    char *words;

    if ((file = fopen("sample.txt", "r")) == NULL) {
        printf("File empty.\n");
        return 0;
    }

    while (strcmp(fgets(lines, MAXSTR, file), END) != 0) { 

        words = strtok(lines, delim);

        while (words != NULL && strcmp(words, STRWORD) != 0 
                             && strcmp(words, SECTION) != 0 
                             && strcmp(words, ENDWORD) != 0) {

            printf("%s\n", words);
            words = strtok(NULL, delim);
        }
    }
    fclose(fileUrl);
    return 0; 
}

1 个答案:

答案 0 :(得分:1)

您的想法是正确的。关键是在找到第一个要读取的 "Section-X" 时设置一个标志,然后在设置该标志时,标记每一行直到找到关闭的 "Section-X",此时退出读取循环.

您可以随意检查 "Section-X",使用整行,或仅使用 "Section-X" 标识符(我在下面选择)。要定位 "Section-X" 文本,只需使用 strrchr() 查找每行中的最后一个空格,然后从下一个字符到您所在部分的行尾进行比较,例如

#include <stdio.h>
#include <string.h>

#define MAXC 1024

int main (int argc, char **argv) {
    
    if (argc < 2) { /* validate 1 arg givent for filename */
        fprintf (stderr, "usage: %s file [\"Section-X\" (default: 2)]\n", argv[0]);
        return 1; 
    }
    
    const char *section = argc > 2 ? argv[2] : "Section-2",     /* set section */
        *delim = " ";
    char line[MAXC];
    int found = 0;                      /* found flag, 0-false, 1-true */
    FILE *fp = fopen (argv[1], "r");    /* open file */
    
    if (!fp) {  /* validate file open for reading */
        perror ("fopen-fp");
        return 1;
    }
    
    while (fgets (line, MAXC, fp)) {                /* read each line */
        line[strcspn (line, "\n")] = 0;             /* trim \n from end */
        char *p = strrchr(line, ' ');               /* pointer to last space */
        if (p && strcmp (p + 1, section) == 0) {    /* compare "Section-X" */
            if (found++)                            /* check/set found flag */
                break;                              /* break loop if 2nd "Section-X" */
            continue;
        }
        if (found) {    /* if found set, tokenize each line */
            for (p = strtok (line, delim); p; p = strtok (NULL, delim))
                puts (p);
        }
    }
}

示例使用/输出

将您的输入存储在文件 dat/sections.txt 中并读取默认值 "Section-2"

$ ./bin/read_sections dat/sections.txt
Other
words
are
also
here.

阅读"Section-1"

$ ./bin/read_sections dat/sections.txt "Section-1"
Some
words
are
here.

检查一下,如果您有问题,请告诉我。

相关问题