处理fscanf()

时间:2012-12-23 21:15:43

标签: c arrays string data-structures scanf

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

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

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

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

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

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

2 个答案:

答案 0 :(得分: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'
}

答案 1 :(得分: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)为它给出的字符串副本分配足够的空间。