【问题标题】:C - Reading multiline from text fileC - 从文本文件中读取多行
【发布时间】:2011-10-24 20:22:21
【问题描述】:

这很可能是一个愚蠢的问题! 我有一个填充了随机数的文本文件,我想将这些数字读入一个数组。

我的文本文件如下所示:

1231231 123213 123123
1231231 123213 123123
0

1231231 123213 123123
1231231 123213 123123
0

以此类推.. numbere 以0 结尾

这是我迄今为止尝试过的:

FILE *file = fopen("c:\\Text.txt", "rt");
char line[512];

if(file != NULL)
{
    while(fgets(line, sizeof line, file) != NULL)
    {
        fputs(line, stdout);
    }
    fclose(file);
}

这显然不起作用,因为我将每一行读入同一个变量。

我如何读取这些行以及当该行获得以 0 结尾的行时,然后将该段文本存储到一个数组中?

感谢所有帮助。

【问题讨论】:

  • 所以你想要一个字符串数组?
  • @Lars:你想要文本字符串,而不是实际数字?
  • @Kerrek SB - 首先,我需要阅读一段文本,然后在阅读一段文本后,我想对其进行标记,我不想在孔图已被读取。
  • 好的,但是在标记化之后,您是要将字符串解析为数字还是将它们保留为字符串?
  • @Kerrek SB - 我想将它们解析为数字,请查看我上面的更新评论!

标签: c file text


【解决方案1】:

您只需将从文件中读取的数字存储在某个永久存储中!此外,您可能想要解析各个数字并获得它们的数字表示。所以,三个步骤:

  1. 分配一些内存来保存数字。数组数组看起来是一个有用的概念,每个数字块对应一个数组。

  2. 使用strtok将每一行标记为对应一个数字的字符串。

  3. 使用atoistrtol 将每个数字解析为整数。

这里有一些示例代码可以帮助您入门:

FILE *file = fopen("c:\\Text.txt", "rt");
char line[512];

int ** storage;
unsigned int storage_size = 10; // let's start with something simple
unsigned int storage_current = 0;

storage = malloc(sizeof(int*) * storage_size); // later we realloc() if needed

if (file != NULL)
{
    unsigned int block_size = 10;
    unsigned int block_current = 0;

    storage[storage_current] = malloc(sizeof(int) * block_size); // realloc() when needed

    while(fgets(line, sizeof line, file) != NULL)
    {
        char * tch = strtok (line, " ");
        while (tch != NULL)
        {
            /* token is at tch, do whatever you want with it! */

            storage[storage_current][block_current] = strtol(tch, NULL);

            tch = strtok(NULL, " ");

            if (storage[storage_current][block_current] == 0)
            {
                ++storage_current;
                break;
            }

            ++block_current;

            /* Grow the array "storage[storage_current]" if necessary */
            if (block_current >= block_size)
            {
                block_size *= 2;
                storage[storage_current] = realloc(storage[storage_current], sizeof(int) * block_size);
            }
        }

        /* Grow the array "storage" if necessary */
        if (storage_current >= storage_size)
        {
            storage_size *= 2;
            storage = realloc(storage, sizeof(int*) * storage_size);
        }
    }
}

最后,你需要释放内存:

for (unsigned int i = 0; i <= storage_current; ++i)
    free(storage[i]);
free(storage);

【讨论】:

  • 我认为你需要在几个地方s/int/storage/g
  • @user:是的,已经发现了 - 谢谢! realloc 会移动现有内存吗?
  • 确实如此。 realloc 需要注意的一点是,p=realloc(p,sz) 是一种反模式,因为如果 p!=NULLrealloc 返回 NULL,代码将会泄漏。不过,这可能并不重要。
  • @user:是的,这很重要,真正的代码应该检查所有对malloc()/realloc()的调用。我不会更改示例,以免混淆演示文稿,但您是绝对正确的。
  • 这有问题吗:storage = malloc(sizeof(int*) * storage_size),因为我收到一个错误,说 void *" 不能分配给“int **”类型的实体?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
  • 2020-02-24
  • 1970-01-01
相关资源
最近更新 更多