【问题标题】:Split a file content or a string to an array of string of the SAME size将文件内容或字符串拆分为相同大小的字符串数组
【发布时间】:2015-02-28 22:25:06
【问题描述】:

我需要关于 C 编程的建议。我需要将文件(或其内容)拆分为一个字符数组。我在互联网上发现了不同的解决方案,可以将文件拆分为不同的较小文件,也可以将字符串拆分为数组,但使用带有 strtok() 的分隔符

问题是:我需要将其拆分并存储到一个 char 数组中,但每个元素必须具有相同的大小(64 位)

你们知道我该怎么做吗(使用标准 C 库)? 任何建议将不胜感激。

更清楚一点:我设法将一个文件拆分为几个相同大小的较小文件示例:

Zeddis@localhost $> ls -la
-rw-rw-r--. 1 Zeddis Zeddis     64 28 févr. 23:17 filepart_number_10
-rw-rw-r--. 1 Zeddis Zeddis     64 28 févr. 23:17 filepart_number_11
-rw-rw-r--. 1 Zeddis Zeddis     64 28 févr. 23:17 filepart_number_12
[...]

但我需要将内容存储到数组中,而不是文件中,并且每个元素的大小必须为 64。

假设我有一个 2mb 的 .txt 文件,我需要将其内容存储到其元素大小为 64 位的数组中。

test.txt content : qwertyuiopasdfgh"

a[0] = "qwer"
a[1] = "tyui"
a[2] = "opas"
a[3] = "dfgh"

[...]

注意:我在示例中使用了 char,但它可以是 int 或任何东西,我只需要每个元素的大小相同。 我希望你能理解,因为英语不是我的母语。

【问题讨论】:

  • 我需要将它存储到一个 char 数组中,但每个元素必须具有相同的大小(64 位)。 - 这是什么意思? chars 总是 8 位。
  • 我的意思是,如果我有一个 test.txt 文件 zith 随机内容(如 /dev/urandom 的猫),我需要将其内容存储到每个元素具有相同大小的数组中.我可以假设 a[)] = "aaaa" 和 a[1] = "q2#@" 具有相同的大小吗?这也许是让我困惑的事情
  • 如果内容的长度不是64的倍数怎么办?注意:是的,“aaaa”和“q2#@”的大小相同,因为它们都有 5 个字符,并且 char 的大小是固定的。
  • 你的意思是64位吗?您的示例 a[0] = "qwer" 显示 32 位(四个 8 位字节),您的文件示例显示 64 个 byte 文件。
  • C 中的字符串是\0 分隔的字符数组。文件里面可以有\0 字符。在给您答案之前,我们该如何应对?在您的示例中,假设您的文件内容是:qwert\0yuiopasdfgh 我们如何处理ty 之间的\0 字符?

标签: c arrays string


【解决方案1】:

考虑下面的例子,也许它会有用:

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

int main(int argc, char * argv[])
{
    FILE * f;
    int64_t buffer;
    // check that file name is given and file is available
    if(argc == 2)
    {
        f = fopen(argv[1], "rb");
        if( f == NULL )
        {
            printf("File %s cannot be read!\n", argv[1]);
            return 1;
        }
    }
    else
        {
            printf("File name needed!\n");
            return 1;
        }
    // reading from file to buffer in the loop
    while( !feof(f) )
    {
        if( 1 == fread(&buffer, sizeof(buffer), 1, f) )
        {
            // do something with buffer (e.g. copy value to array)
            // ...
        }
    }
    // ...
    fclose(f);
}

编辑:

对于char[8](64位)数组作为缓冲区的情况:

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

int main(int argc, char * argv[])
{
    FILE * f;
    char charbuf[8];
    int cnt;
    // check that file name is given and file is available
    if(argc == 2)
    {
        f = fopen(argv[1], "rb");
        if( f == NULL )
        {
            printf("File %s cannot be read!\n", argv[1]);
            return 1;
        }
    }
    else
        {
            printf("File name needed!\n");
            return 1;
        }
    // reading from file to buffer in the loop
    while( !feof(f) )
    {
        if( 8 == fread(charbuf, sizeof(char), 8, f) ) // only if 8 bytes was read
        {
            // do something with buffer
            // e.g. output all chars
            for(cnt = 0; cnt < 8; cnt++)
                putchar(charbuf[cnt]);
            putchar('\n');
        }
    }
    // ...
    fclose(f);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-22
    • 2020-09-29
    • 2017-02-02
    • 2016-04-01
    相关资源
    最近更新 更多