简体   繁体   English

从文件(.txt)中读取并保存到char * C中

[英]Read from a file (.txt) and save into a char* C

I have a file.txt that it contais, for example, "This is a txt file" (this content can be variable) and I need a function that reads file.txt and save its content into a char*. 我有一个包含文件名的file.txt,例如“这是一个txt文件”(此内容可以是变量),并且我需要一个函数来读取file.txt并将其内容保存到char *中。

file.txt contains -> "This is a .txt file" file.txt包含->“这是一个.txt文件”

and I need a char *readedContent that contains "This is a .txt file". 并且我需要一个包含“这是一个.txt文件”的char * readedContent。

First, I save the content of the char *str (str contains "this is a .txt file") into a "file.txt" and then I try to get the string from this file but the string have more chars than "This is a .txt file". 首先,我将char * str(str包含“这是一个.txt文件”)的内容保存到“ file.txt”中,然后尝试从该文件中获取字符串,但该字符串的字符数比“ This是.txt文件”。 (Often add characters like spaces or @,?) (通常添加空格或@ ,?等字符)

My function is: 我的职能是:

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

The problem that I have is that in the char *readedContent I have more chars than the file.txt's content. 我的问题是在char * readedContent中,我的字符比file.txt的内容多。

Thanks very much. 非常感谢。

The problem that I have is that in the char *readedContent I have more chars than the file.txt 's content. 我的问题是在char *readedContent我的字符比file.txt的内容多。

The most likely reason why you get more bytes than there are characters in the file is the encoding of your file: fread() reads the file byte-for-byte, so if file's encoding uses multiple bytes for some code points, your buffer is going to contain multiple bytes for one or more characters. 您得到的字节数多于文件中字符数的最可能的原因是文件的编码fread()逐字节读取文件,因此,如果文件的编码对某些代码点使用多个字节,则缓冲区是将包含一个或多个字符的多个字节。

To verify this theory and fix the problem, write a short program that writes the bytes of your intended message, "This is a .txt file" , into a text file using the fwrite() API. 为了验证这一理论并解决问题,请编写一个简短的程序,使用fwrite() API将所需消息的字节"This is a .txt file"写入文本文件。 A file written in this way should read correctly with fread() . 以这种方式编写的文件应使用fread()正确读取。

Try 尝试

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

Instead of rewind(f2) 而不是rewind(f2)

Also move readedContent[len] = '\\0'; 还要移动readedContent[len] = '\\0'; after fread 恐惧之后

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

Output on linux 在Linux上输出

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

working correctly on linux 在Linux上正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM