0

我有一个结构简单的文件:
{number}\#
{some_text_with_lines}
\#

问题是我想要一个程序读取这个文件中的每个条目。这意味着这种结构不止一次出现。

所以,我的第一个想法是创建一个结构数组,某种struct my_struct abc[MAX];. MAX是一个预处理器定义。

我希望将fscanf{number} 存储为数组的编号,并将 {some_text_with_lines} 存储为结构数组中的字符串。所以每个数组都有自己的字符串。

思路是 fscanf 读取以下格式:%d#\r\n%s\r\n#r\n,并使用第一个整数%d作为数组的编号。像 &abc[%d] 这样的东西。
虽然我知道它的语法是错误的,但我不知道如何读取这个数字 %d 并将其用作数组的编号。另外,%s 有问题,它没有\0?

所以,我需要一些帮助,在此先感谢。

4

2 回答 2

1

做这样的事情:

int index;
char array[1024][1024];               // big array :)

while(fscanf(file, "%d#\r\n", &index) == 1) {
  fscanf(file, "%s\r\n", array[index]);
  int c; 
  while ((c = fgetc(file)) != EOF && c != '\r') { }
  fgetc(file);                          // read '\n'
}
于 2012-12-23T22:30:32.723 回答
1

此代码有效。它将接受文件中的任意段落列表,并且每个段落中的行集也可以是任意长度。

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

struct data
{
    int     number;
    char  **lines;
    int     num_lines;
    int     max_lines;
};

int main(void)
{
    struct data *info = 0;
    int   num_entries = 0;
    int   max_entries = 0;
    char line[4096];

    while (fgets(line, sizeof(line), stdin) != 0)
    {
        int number;
        char hash;
        if (sscanf(line, "%d%c", &number, &hash) != 2 || hash != '#')
        {
            fprintf(stderr, "Format error (number# expected): %s", line);
            return(1);
        }

        if (max_entries >= num_entries)
        {
            int new_size = (max_entries + 2) * 2;
            struct data *new_info = realloc(info, new_size * sizeof(*new_info));
            if (new_info == 0)
            {
                fprintf(stderr, "Out of memory\n");
                return(1);
            }
            info = new_info;
            max_entries = new_size;
        }

        struct data *curr = &info[num_entries];
        curr->number    = number;
        curr->lines     = 0;
        curr->max_lines = 0;
        curr->num_lines = 0;

        while (fgets(line, sizeof(line), stdin) != 0)
        {
            char *p = strchr(line, '\n');
            if (p == 0)
            {
                fprintf(stderr, "Format error: no newline? (%s)\n", line);
                return(1);
            }
            *p = '\0';
            if (strcmp(line, "#") == 0)
                break;
            if (curr->max_lines >= curr->num_lines)
            {
                int new_size = (curr->max_lines + 2) * 2;
                char **new_lines = realloc(curr->lines, new_size * sizeof(*new_lines));
                if (new_lines == 0)
                {
                    fprintf(stderr, "Out of memory\n");
                    return(1);
                }
                curr->lines = new_lines;
                curr->max_lines = new_size;
            }
            curr->lines[curr->num_lines] = strdup(line);
            if (curr->lines[curr->num_lines] == 0)
            {
                fprintf(stderr, "Out of memory\n");
                return(1);
            }
            curr->num_lines++;
        }

        num_entries++;
    }

    for (int i = 0; i < num_entries; i++)
    {
        printf("%d#\n", info[i].number);
        for (int j = 0; j < info[i].num_lines; j++)
            printf("  %d: %s\n", j, info[i].lines[j]);
    }

    return(0);
}

给定输入文件:

13#
Unlucky for some
#
20121221#
The end of the world?
No, it seems that we survived yet another doomsday!
#
18#
More lines,
And more lines still.
The verse is weird.
The terse is worse.
#
19#
As for one,
Then another,
It is still too short
For comfort,
But the fifth line shall trigger
an extra reallocation.
#
20#
All for one,
And one for all!
The Three Musketeers?
Nay, D'Artagnan, the Four Musketeers.
Yahoo! Bing?  Google?
#

它给出了输出:

13#
  0: Unlucky for some
20121221#
  0: The end of the world?
  1: No, it seems that we survived yet another doomsday!
18#
  0: More lines,
  1: And more lines still.
  2: The verse is weird.
  3: The terse is worse.
19#
  0: As for one,
  1: Then another,
  2: It is still too short
  3: For comfort,
  4: But the fifth line shall trigger
  5: an extra reallocation.
20#
  0: All for one,
  1: And one for all!
  2: The Three Musketeers?
  3: Nay, D'Artagnan, the Four Musketeers.
  4: Yahoo! Bing?  Google?

毫无疑问,代码还有改进的空间,但它似乎可以工作(尽管它在退出之前不会释放内存)。它假定有一个函数char *strdup(const char *str)为给定字符串的副本分配足够的空间。

于 2012-12-24T02:50:47.767 回答