C++ fwrite和write的主要区别是什么?
我目前正在C++ fwrite和write的主要区别是什么?,c++,c,C++,C,我目前正在C中编写一个回调函数: static size_t writedata(void *ptr, size_t size, size_t nmemb, void *stream){ size_t written = fwrite(ptr, size, nmemb, (FILE)*stream); return written; } 此函数将在另一个函数中使用,该函数执行HTTP请求,检索请求并将其写入本地计算机。该writedata功能将用于后面的部分。
C
中编写一个回调函数:
static size_t writedata(void *ptr, size_t size, size_t nmemb, void *stream){
size_t written = fwrite(ptr, size, nmemb, (FILE)*stream);
return written;
}
此函数将在另一个函数中使用,该函数执行
HTTP
请求,检索请求并将其写入本地计算机。该writedata
功能将用于后面的部分。整个操作必须是多线程的
,因此我在写入
和写入
之间存在疑问。有人能帮我概括一下C
中write()
和fwrite()
之间的区别,这样我就可以选择最适合我的问题的方法了?fwrite
写入文件*
,即(可能)缓冲的stdio
流。它是由ISO C标准规定的。此外,在POSIX系统上,在一定程度上
write
是基于POSIX标准中描述的文件描述符的低级API。它不知道缓冲。如果要在文件*
上使用它,请使用fileno
获取其文件描述符,但在尝试写入之前,请确保手动锁定并刷新流
除非您知道自己在做什么,否则请使用write
。write
函数是程序对操作系统的调用,因此它比write
慢。它还缺少缓冲,这使得它的速度更加慢,因为缓冲的原理表明:“处理小文件的许多部分比处理大文件更快。”同样重要的是write
不是c标准的一部分,因此您可能不会发现它是非POSIX系统的,而且(很少)适当的使用方法会有所不同。您还应该知道,fwrite和fread有时是使用write
和read
实现的(一个简单的实现可以在K&R的Unix一章中找到)
另一件值得注意的事情是,读取
和写入
使用,但fread和fwrite使用文件指针,这些指针实际上包含文件描述符和有关打开文件的其他信息。一个非常明显的区别是写入是原子的,而fwrite不是
使用:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main() {
if (fork() == 0) {
FILE* h = fopen("file.txt", "a");
char* line =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n";
for (int i = 0; i < 10000; i++) {
if (write(fileno(h), line, strlen(line)) != strlen(line)) {
perror("Could not append line to file");
exit(1);
}
}
if (fclose(h) != 0) {
perror("Could not close file");
exit(1);
}
} else {
FILE* h = fopen("file.txt", "a");
char* line =
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n";
for (int i = 0; i < 10000; i++) {
if (write(fileno(h), line, strlen(line)) != strlen(line)) {
perror("Could not append line to file");
exit(1);
}
}
if (fclose(h) != 0) {
perror("Could not close file");
exit(1);
}
}
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main() {
if (fork() == 0) {
FILE* h = fopen("file.txt", "a");
char* line =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n";
for (int i = 0; i < 10000; i++) {
if (fwrite(line, 1, strlen(line), h) != strlen(line)) {
perror("Could not append line to file");
exit(1);
}
}
if (fclose(h) != 0) {
perror("Could not close file");
exit(1);
}
} else {
FILE* h = fopen("file.txt", "a");
char* line =
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n";
for (int i = 0; i < 10000; i++) {
if (fwrite(line, 1, strlen(line), h) != strlen(line)) {
perror("Could not append line to file");
exit(1);
}
}
if (fclose(h) != 0) {
perror("Could not close file");
exit(1);
}
}
return 0;
}
使用:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main() {
if (fork() == 0) {
FILE* h = fopen("file.txt", "a");
char* line =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n";
for (int i = 0; i < 10000; i++) {
if (write(fileno(h), line, strlen(line)) != strlen(line)) {
perror("Could not append line to file");
exit(1);
}
}
if (fclose(h) != 0) {
perror("Could not close file");
exit(1);
}
} else {
FILE* h = fopen("file.txt", "a");
char* line =
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n";
for (int i = 0; i < 10000; i++) {
if (write(fileno(h), line, strlen(line)) != strlen(line)) {
perror("Could not append line to file");
exit(1);
}
}
if (fclose(h) != 0) {
perror("Could not close file");
exit(1);
}
}
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main() {
if (fork() == 0) {
FILE* h = fopen("file.txt", "a");
char* line =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n";
for (int i = 0; i < 10000; i++) {
if (fwrite(line, 1, strlen(line), h) != strlen(line)) {
perror("Could not append line to file");
exit(1);
}
}
if (fclose(h) != 0) {
perror("Could not close file");
exit(1);
}
} else {
FILE* h = fopen("file.txt", "a");
char* line =
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n";
for (int i = 0; i < 10000; i++) {
if (fwrite(line, 1, strlen(line), h) != strlen(line)) {
perror("Could not append line to file");
exit(1);
}
}
if (fclose(h) != 0) {
perror("Could not close file");
exit(1);
}
}
return 0;
}
不过,需要注意的是,您可能不应该从多个线程访问文件*结构(包括但不限于调用fread/fwrite),而使用低级文件描述符通常更安全。。。但如果没有一些同步,这仍然不是一个好主意:)@fred foo我很好奇,如果我用一个线程多次调用write(),而没有锁定或刷新,会发生什么。假设fwrite()是线程安全的,而write()不是线程安全的,我认为write()在写入ramdrive时会比fwrite()快。但是,我的测试表明不是这样——大约慢20倍。有什么想法吗?缺少缓冲并不会使它变慢——如果你正在为例如写固定大小的内存块(或者已经在管理你自己的缓冲),缓冲可能会带来不必要的开销。最好说,如果您想从缓冲写入中获益,最好使用fwrite
。