【问题标题】:Char* parse error from char array字符数组中的字符 * 解析错误
【发布时间】:2017-03-15 21:46:35
【问题描述】:

我读取了一个文件并存储了所有这样的字符:

void ReadFile()
{
    int c;
    FILE *file;

    int string_size;
    file = fopen("/userFiles/ex.txt", "r");
    char * content;
    if (file)
    {

        // Seek the last byte of the file
        fseek(file, 0, SEEK_END);
        // Offset from the first to the last byte, or in other words, filesize
        string_size = ftell(file);
        // go back to the start of the file
        rewind(file);
        // Allocate a string that can hold it all
        content = malloc((string_size + 1) * sizeof(char));

        int i = 0;

        while ((c = getc(file)) != EOF)
        {
            //printf("%c",(char) c);
            content[i] = (char) c;
            i++;
        }

        content[string_size] = '\0';
        printf("content: %s",content);
        fclose(file);

    }
    else
    {
        printf("not load\n");
    }

}

问题是,如果我阅读每个字符,我已经获得了文件的内容,但如果我这样做了:

printf("content: %s",content);

我只得到一个符号而不是文本,而我需要在 json 回复的参数中传递带有正确文本的内容变量。

这是文件的第一行(CRC32):

�ex.txt k��X�?xml version="1.0" encoding="utf-8"?

【问题讨论】:

  • 它可能不会解决问题,但使用fread() 而不是一次读取一个字符。 sizeof(char) 始终为 1,这是强制性的。
  • 我用过 fread 但同样的问题
  • 我认为你的问题是输入文件。因为这段代码很好。
  • 该文件不是一个简单的txt文件,它看起来像xml语法但开头有一个CRC32,我添加一个例子
  • 问题不在于读数,而在于您输出它们的方式。一种可能是检查每个字节值并用“。”打印它们。当它们不可打印时。这有点类似于 hexdump -C 会做的事情。 man ascii 可以为您提供一个起点,但请记住,像 unicode 这样的东西会使“可打印”检测变得相当复杂。

标签: c arrays json char readfile


【解决方案1】:

我编译并运行了以下代码,执行时没有出现大问题。

我使用的可编译版本是(cmdline: gcc main.c -Wall -Wextra -o main):

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

    int main(void)
    {
            int c;
            FILE *file;

            int string_size;
            file = fopen("plop", "r");
            char * content;
            if (file == NULL)
            {
                    perror("fprintf");
                    return 1;
            }

            // Seek the last byte of the file
            fseek(file, 0, SEEK_END);
            // Offset from the first to the last byte, or in other words, filesize
            string_size = ftell(file);
            // go back to the start of the file
            rewind(file);
            // Allocate a string that can hold it all
            content = malloc((string_size + 1) * sizeof(char));

            int i = 0;

            while ((c = getc(file)) != EOF)
            {
                    //printf("%c",(char) c);
                    content[i] = (char) c;
                    i++;
            }

            content[string_size] = '\0';

            printf("content: %s", content);

            return 0;
    }

也许您的文件包含二进制内容?
你提到的印刷符号是什么?

【讨论】:

  • content 未初始化、未定义行为时,您正在打印它。
  • 我的文件开头包含一个 CRC32,所以它有特殊字符,你认为这可能是原因吗?
  • @user2622016:已编辑,但对于未在 OP 原始版本中检查的 malloc 也是如此...... --- to jof III:当然,例如,特殊字符可能包含 \ r 和一些空格并覆盖输出的其余部分。你能附上你的文件供检查吗?
【解决方案2】:

我认为您应该在内容字符串周围使用引号 ""

printf("content: \"%s\"",content);

【讨论】:

  • 我做到了,同样的问题
猜你喜欢
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多