fread 和 fgets 规范之间的区别?
Difference between specifications of fread and fgets?
从文件读入时,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 标准:
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.
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.
这段代码可能会让您更清楚。它只是以块的形式复制文件。
#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;
}
从文件读入时,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 标准:
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.
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.
这段代码可能会让您更清楚。它只是以块的形式复制文件。
#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;
}