c - 从中​​文本文件中读取多行 [英] C - Reading multiline from text file

查看:187
本文介绍了c - 从中​​文本文件中读取多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is most likely a dumb question! I have a text file filled with random numbers and I would like to read these numbers into an array.

我的文本文件看起来像这样:

My text file looks like this:

1231231 123213 123123
1231231 123213 123123
0

1231231 123213 123123
1231231 123213 123123
0

和等..这件作品numberse与 0

And so on.. The piece of numberse ends with 0

这是我到目前为止已经试过:

This is what I have tried so far:

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);
}

这也显然不工作,因为我读的每一行到相同的变量。

This does clearly not work, since I read each line into the same variable.

我怎么能读线和当行得到它以0结尾的行,那一段文字,存储到一个数组?

How can I read the lines and when the line gets the line where it ends with 0, then store that piece of text into an array?

所有帮助AP preciated。

All help is appreciated.

推荐答案

您只需要存储您从文件中的某些永久存储读取的数字!此外,您可能要分析各个编号并获取其数值重新presentation。因此,三个步骤:

You just have to store the numbers that you read from the file in some permanent storage! Also, you probably want to parse the individual numbers and obtain their numerical representation. So, three steps:


  1. 分配一些内存来容纳的人数。数组的数组看起来像一个有用的概念,一个阵列换号的每个块。

  1. Allocate some memory to hold the numbers. An array of arrays looks like a useful concept, one array for each block of numbers.

标记化每条线成对应一个号码每个,使用字符串 strtok的

Tokenize each line into strings corresponding to one number each, using strtok.

解析每个号码到使用的atoi 与strtol

Parse each number into an integer using atoi or strtol.

下面是一些例子code,让你开始:

Here's some example code to get you started:

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);
        }
    }
}

在最后,你需要释放内存:

In the end, you need to free the memory:

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

这篇关于c - 从中​​文本文件中读取多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆