【问题标题】:Read from a file (.txt) and save into a char* C从文件 (.txt) 读取并保存到 char* C
【发布时间】:2013-05-08 16:49:02
【问题描述】:

我有一个包含的 file.txt,例如,“这是一个 txt 文件”(此内容可以是可变的),我需要一个读取 file.txt 并将其内容保存到 char* 中的函数。

file.txt 包含 -> “这是一个 .txt 文件”

我需要一个包含“这是一个 .txt 文件”的 char *readedContent。

首先,我将 char *str(str 包含“这是一个 .txt 文件”)的内容保存到“file.txt”中,然后我尝试从该文件中获取字符串,但字符串有更多字符而不是“这是一个 .txt 文件”。 (经常添加空格或@,?)

我的功能是:

char *special_char_remplace(char *str){


    FILE *f1;
    f1 = fopen("file.txt","w+"); 
    fprintf(f1,"%s", str);
    fclose(f1);

    size_t len, bytesRead;
    char *readedContent;
    FILE* f2;

    f2 = fopen("file.txt", "rb");

    fseek(f2, 0, SEEK_END);
    len = ftell(f2);
    rewind(f2);

    readedContent = (char*) malloc(sizeof(char) * len + 1);
    readedContent[len] = '\0'; // Is needed only for printing to stdout with printf

    bytesRead = fread(readedContent, sizeof(char), len, f2);

    printf("STRING: %s\n",  readedContent);

    fclose(f2);

    return readedContent;
}

我遇到的问题是在 char *readedContent 中我的字符比 file.txt 的内容多。

非常感谢。

【问题讨论】:

  • 要阅读文本内容,您可以使用fopen("file.txt", "rt");
  • 您能否举一个文件内容与您的程序读取的readedContent 的示例?
  • 您在 Windows 上吗?您是否遇到了 ^Z (control-Z) 标记文件末尾的问题,但之后有多达 255 个额外字节才能到达块的末尾?如果您使用"r""rt" 模式打开文件会发生什么?
  • 您最好将fread 更改为fread(readedContent, len, 1, f2) 以避免bytesRead 小于len 的可能性。不过,这不会导致您描述的问题。
  • @EAGER_STUDENT 不太正确,“rt”正在为您的数据添加一些额外的内容(例如取决于操作系统的新行),而不是对文件内容进行精确的字节复制

标签: c file memory


【解决方案1】:

我遇到的问题是char *readedContent 中的字符比file.txt 的内容多。

您获得的字节数多于文件中字符数的最可能原因是文件的encodingfread() 逐字节读取文件,因此如果文件的编码对某些代码使用多个字节点,您的缓冲区将包含一个或多个字符的多个字节。

要验证这一理论并解决问题,请编写一个简短的程序,使用 fwrite() API 将预期消息的字节 "This is a .txt file" 写入文本文件。以这种方式写入的文件应该使用fread() 正确读取。

【讨论】:

  • 我尝试过的事情是:我编写了一个函数,它接收一个 char *string 并使用 fwrite() 写入一个文件,然后我像在帖子中展示的那样阅读它,但是它不起作用,当我从文件中读取时,我的字符比我写的要多...我不明白...非常感谢
  • @dikerex 您能否用您用来编写file.txt 内容的程序的代码更新问题?
  • @dikerex 你在printf中的str前面多了一个星号,替换成fprintf(f1,"%s", str);,程序就会按预期运行。
  • @dikerex 你能不能把最后一个printf 替换成printf("STRING: %s %d %d\n", readedContent, len, strlen(str)); 看看打印了什么?
  • STRING:这是一个 .txt 文件 35 19
【解决方案2】:

试试

fseek(f2, 0L, SEEK_END);
long tmpSize = ftell(f2);
fseek(f2, 0L, SEEK_SET);    

而不是rewind(f2)

在 fread 之后也移动 readedContent[len] = '\0';

【讨论】:

  • 根据其man pagerewind(f2)等价于fseek(f2, 0L, SEEK_SET)。虽然我同意移动 readedContent[len] = '\0' 可能会更清晰,但应该没有任何区别。
  • 不使用rewind有什么好处?以及为什么要更改作业的位置?
  • @simonc 好的...我宁愿将它与双 fseek 一起使用,它对我来说更具可读性。我从没用过倒带
  • @Martin Perry 为什么我们不应该使用rewind(f2)
  • 我试过了,但这比以前更有效。非常感谢。
【解决方案3】:
 #include<stdio.h>
    #include<stdlib.h>
    int main()
    {

        char *str = "this is my file";
        FILE *f1;
        f1 = fopen("file.txt","w");
        fprintf(f1,"%s",str);
        fclose(f1);
        //string written in file.txt

        size_t len, bytesRead;
        char *readedContent;
        FILE* f2;
        f2 = fopen("file.txt", "rb");
        fseek(f2, 0, SEEK_END);
        len = ftell(f2);
        //will need it for length
        rewind(f2);
        readedContent = (char*) malloc(sizeof(char) * len + 1);
        readedContent[len] = '\0'; // Is needed only for printing to stdout with printf
        bytesRead = fread(readedContent, sizeof(char), len, f2);
        //fread will read it from file and 
        //readed content will be pointed by readedContent pointer
        printf("STRING: %s\n",  readedContent);
        printf("the size is %zd\n",bytesRead);
        fclose(f2);

        return 1;
    }

Linux 上的输出

 STRING: this is my file
`the size is 15`

在 linux 上正常工作

【讨论】:

  • 稍微解释一下你所做的会很棒。还要注意答案的格式。
  • @Volkanİlbeyli 很抱歉,我还是新手,正在熟悉规则和文化
猜你喜欢
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2015-12-06
相关资源
最近更新 更多