读取文件直到C中的一个字符

发布于 2024-08-29 02:36:14 字数 387 浏览 3 评论 0原文

假设我有一个格式如下的文件:

key1/value1
key2/value2
key3/value3
....

假设我有一个数组来保存这些值:

char *data[10][10]

我如何读取此文件并将 key1、key2 和 key3 放入 data[0][0]、data[1][0]、和数据[2][0]。然后将value1、value2、value3分别放入data[0][1]、data[2][1]、data[3][1]中。所以实际上我想单独获取key1-key3的字符串,然后测试'/'字符然后获取value1-3的字符串。顺便说一句,当我将这些输入到文件中时,我包含了“\n”字符,这样您就可以测试它来测试换行符。

Say I have a file with the format:

key1/value1
key2/value2
key3/value3
....

Say I have an array to hold these values:

char *data[10][10]

How would I read this file and get key1, key2, and key3 into data[0][0], data[1][0], and data[2][0]. Then put value1, value2, and value3 into data[0][1], data[2][1], and data[3][1]. So actually I want to get the strings of key1-key3 individually, then test for the '/' character then get the strings of value1-3. By the way, when I'm inputting these into the file, I am including the '\n' character so you could test for that to test for the newline.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2024-09-05 02:36:14

最好的方法是将每行的数据读入缓冲区,然后解析缓冲区。这可以扩展到读取大数据块。

使用fgets 将数据读入缓冲区。

使用 strchr 查找分隔符。

示例:

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

#define MAX_TEXT_LINE_LENGTH 128


int main(void)
{
    FILE *  my_file("data.txt", "r");
    char    text_read[MAX_TEXT_LINE_LENGTH];
    char    key_text[64];
    char    value_text[64];

    if (!my_file)
    {
        fprintf(stderr, "Error opening data file:  data.txt");
        return EXIT_FAILURE;
    }
    while (fgets(text_read, MAX_TEXT_LINE_LENGTH, my_file))
    {
        char * p;
        //----------------------------------------------
        //  Find the separator.
        //----------------------------------------------
        p = strchr('/');

        key_text[0] = '\0';
        value_text[0] = '\0';

        if (p != 0)
        {
            size_t  key_length = 0;
            key_length = p - text_read;

            //  Skip over the separator
            ++p;
            strcpy(value_text, p);

            strncpy(key_text, text_read, key_length);
            key_text[key_length] = '\0';

            fprintf(stdout,
                    "Found, key: \"%s\", value: \"%s\"\n",
                    key_text,
                    value_text);
        }
        else
        {
            fprintf(stdout,
                    "Invalid formatted text: \"%s\"\n",
                    text_read);
        }
    } // End:  while fgets
    fclose(my_file);

    return EXIT_SUCCESS;
}

注意:以上代码未经编译或测试,仅用于说明目的。

The best method is to read the data per line into a buffer, then parse the buffer. This can be expanded to reading in large blocks of data.

Use fgets for reading the data into a buffer.

Use strchr to find the separator character.

Example:

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

#define MAX_TEXT_LINE_LENGTH 128


int main(void)
{
    FILE *  my_file("data.txt", "r");
    char    text_read[MAX_TEXT_LINE_LENGTH];
    char    key_text[64];
    char    value_text[64];

    if (!my_file)
    {
        fprintf(stderr, "Error opening data file:  data.txt");
        return EXIT_FAILURE;
    }
    while (fgets(text_read, MAX_TEXT_LINE_LENGTH, my_file))
    {
        char * p;
        //----------------------------------------------
        //  Find the separator.
        //----------------------------------------------
        p = strchr('/');

        key_text[0] = '\0';
        value_text[0] = '\0';

        if (p != 0)
        {
            size_t  key_length = 0;
            key_length = p - text_read;

            //  Skip over the separator
            ++p;
            strcpy(value_text, p);

            strncpy(key_text, text_read, key_length);
            key_text[key_length] = '\0';

            fprintf(stdout,
                    "Found, key: \"%s\", value: \"%s\"\n",
                    key_text,
                    value_text);
        }
        else
        {
            fprintf(stdout,
                    "Invalid formatted text: \"%s\"\n",
                    text_read);
        }
    } // End:  while fgets
    fclose(my_file);

    return EXIT_SUCCESS;
}

Note: The above code has not been compiled nor tested, but is for illustrative purposes only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文