gpt4 book ai didi

c - fread 和 fgets 规范之间的区别?

转载 作者:太空宇宙 更新时间:2023-11-03 23:37:21 25 4
gpt4 key购买 nike

从文件读入时,fread 和 fgets 有什么区别?我使用相同的 fwrite 语句,但是当我使用 fgets 读取 .txt 文件时它按预期工作,但是当我使用 fread() 时它却没有。

在读取和读取文件时,我已经从 fgets/fputs 切换到 fread/fwrite。我使用 fopen(rb/wb) 读取二进制而不是标准字符。我知道 fread 也会得到/0 空字节,而不仅仅是单行。

  //while (fgets(buff,1023,fpinput) != NULL) //read in from file
while (fread(buff, 1, 1023, fpinput) != 0) // read from file

我希望从一个文件读入一个缓冲区,将缓冲区放在共享内存中,然后让另一个进程从共享内存中读取并写入一个新文件。

当我使用 fgets() 时,它可以按预期处理 .txt 文件,但是当使用 fread 时,它会将 300~ 个字符中的一行添加到缓冲区中,并换行。我这辈子都想不通为什么。

最佳答案

fgets 会在遇到换行符时停止。 fread 没有。所以 fgets 通常只对文本文件有用,而 fread 可以同时用于文本和二进制文件。

来自 C11 标准:

7.21.7.2 The fgets function

The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.

7.21.8.1 The fread function

The fread function reads, into the array pointed to by ptr, up to nmemb elements whose size is specified by size, from the stream pointed to by stream. For each object, size calls are made to the fgetc function and the results stored, in the order read, in an array of unsigned char exactly overlaying the object. The file position indicator for the stream (if defined) is advanced by the number of characters successfully read. If an error occurs, the resulting value of the file position indicator for the stream is indeterminate. If a partial element is read, its value is indeterminate.

这段代码可能会让您更清楚。它只是以 block 的形式复制文件。

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

int main(int argc, char ** argv)
{
if(argc != 3) {
printf("Usage: ./a.out src dst\n");
printf("Copies file src to dst\n");
exit(EXIT_SUCCESS);
}

const size_t chunk_size = 1024;

FILE *in, *out;
if(! (in = fopen(argv[1], "rb"))) exit(EXIT_FAILURE);
if(! (out = fopen(argv[2], "wb"))) exit(EXIT_FAILURE);

char * buffer;
if(! (buffer = malloc(chunk_size))) exit(EXIT_FAILURE);

size_t bytes_read;

do {
// fread returns the number of successfully read elements
bytes_read = fread(buffer, 1, chunk_size, in);

/* Insert any modifications you may */
/* want to do here */

// write bytes_read bytes from buffer to output file
if(fwrite(buffer, 1, bytes_read, out) != bytes_read) exit(EXIT_FAILURE);
// When we read less than chunk_size we are either done or an error has
// occured. This error is not handled in this program.
} while(bytes_read == chunk_size);


free(buffer);
fclose(out);
fclose(in);
}

你在下面的评论中提到你想用它来进行字节交换。好吧,您可以只使用以下代码段。只需将其插入上面代码中指示的位置即可。

for(int i=0; i < bytes_read - bytes_read%2; i+=2) {
char tmp = buffer[i];
buffer[i] = buffer[i+1];
buffer[i+1] = tmp;
}

关于c - fread 和 fgets 规范之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55778516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com