将文件的全部内容读取到c char*,包括新行

将文件的全部内容读取到c char*,包括新行,c,io,C,Io,我正在寻找一个跨平台的Windows+Linux解决方案,将整个文件的内容读入char* 这就是我现在得到的: FILE *stream; char *contents; fileSize = 0; //Open the stream stream = fopen(argv[1], "r"); //Steak to the end of the file to determine the file size fseek(stream, 0L, SEEK_END); fileSize = ft

我正在寻找一个跨平台的Windows+Linux解决方案,将整个文件的内容读入char*

这就是我现在得到的:

FILE *stream;
char *contents;
fileSize = 0;

//Open the stream
stream = fopen(argv[1], "r");

//Steak to the end of the file to determine the file size
fseek(stream, 0L, SEEK_END);
fileSize = ftell(stream);
fseek(stream, 0L, SEEK_SET);

//Allocate enough memory (should I add 1 for the \0?)
contents = (char *)malloc(fileSize);

//Read the file 
fscanf(stream, "%s", contents);     

//Print it again for debugging
printf("Read %s\n", contents);
不幸的是,这将只打印文件中的第一行,因此我假设fscanf在第一个换行符处停止。但是,我想阅读整个文件,包括并保留新行字符。我不希望使用while循环和realloc来手动构造整个字符串,我的意思是必须有一种更简单的方法?

函数fread将从流中读取,而不是在行尾字符处终止

在手册页中,您有:

size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
它以大小为单位读取。

函数fread将从流中读取,而不会在行尾字符处终止

在手册页中,您有:

size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
以大小为单位读取。fread按原样读取所有文件:

 if (fread(contents, 1, fileSize, stream) != fileSize) {
    /* error occurred */
 }
fread按原样读取所有文件:

 if (fread(contents, 1, fileSize, stream) != fileSize) {
    /* error occurred */
 }
我有这个:

ssize_t filetomem(const char *filename, uint8_t **result)
{ 
    ssize_t size = 0;
    FILE *f = fopen(filename, "r");
    if (f == NULL) 
    { 
        *result = NULL;
        return -1;
    } 
    fseek(f, 0, SEEK_END);
    size = ftell(f);
    fseek(f, 0, SEEK_SET);
    *result = malloc(size);
    if (size != fread(*result, sizeof(**result), size, f)) 
    { 
        free(*result);
        return -2;
    } 

    fclose(f);
    return size;
}
返回值的含义:

正或0:已成功读取文件 负一:无法打开文件可能没有这样的文件 减二:fread失败了 我有这个:

ssize_t filetomem(const char *filename, uint8_t **result)
{ 
    ssize_t size = 0;
    FILE *f = fopen(filename, "r");
    if (f == NULL) 
    { 
        *result = NULL;
        return -1;
    } 
    fseek(f, 0, SEEK_END);
    size = ftell(f);
    fseek(f, 0, SEEK_SET);
    *result = malloc(size);
    if (size != fread(*result, sizeof(**result), size, f)) 
    { 
        free(*result);
        return -2;
    } 

    fclose(f);
    return size;
}
返回值的含义:

正或0:已成功读取文件 负一:无法打开文件可能没有这样的文件 减二:fread失败了
像这样的东西,可能是吗

FILE *stream;
char *contents;
fileSize = 0;

//Open the stream. Note "b" to avoid DOS/UNIX new line conversion.
stream = fopen(argv[1], "rb");

//Seek to the end of the file to determine the file size
fseek(stream, 0L, SEEK_END);
fileSize = ftell(stream);
fseek(stream, 0L, SEEK_SET);

//Allocate enough memory (add 1 for the \0, since fread won't add it)
contents = malloc(fileSize+1);

//Read the file 
size_t size=fread(contents,1,fileSize,stream);
contents[size]=0; // Add terminating zero.

//Print it again for debugging
printf("Read %s\n", contents);

//Close the file
fclose(stream);
free(contents);

像这样的东西,可能是吗

FILE *stream;
char *contents;
fileSize = 0;

//Open the stream. Note "b" to avoid DOS/UNIX new line conversion.
stream = fopen(argv[1], "rb");

//Seek to the end of the file to determine the file size
fseek(stream, 0L, SEEK_END);
fileSize = ftell(stream);
fseek(stream, 0L, SEEK_SET);

//Allocate enough memory (add 1 for the \0, since fread won't add it)
contents = malloc(fileSize+1);

//Read the file 
size_t size=fread(contents,1,fileSize,stream);
contents[size]=0; // Add terminating zero.

//Print it again for debugging
printf("Read %s\n", contents);

//Close the file
fclose(stream);
free(contents);

工作非常完美,请注意,您不需要大小,因为您已经有了文件大小。接受最清楚的解释!您确实需要size\u t size,因为无论出于何种原因,您都不需要读取所需的字节数。您分配的缓冲区将不需要任何超出读取区块的内容,而是用零填充。。。而且我们真的不想打印垃圾:它在Windows上的结果很糟糕,在UNIX上可能会对终端造成不好的影响。工作非常好,请注意,您不需要大小,因为您已经有了文件大小。接受最清楚的解释!您确实需要size\u t size,因为无论出于何种原因,您都不需要读取所需的字节数。您分配的缓冲区将不需要任何超出读取区块的内容,而是用零填充。。。我们真的不想打印垃圾:它在Windows上的结果很糟糕,在UNIX上可能会对终端造成不好的影响。