fwrite会阻塞嘛
时间: 2023-12-22 18:29:23
浏览: 180
fwrite函数不会阻塞。它是一个用于将数据写入文件的C库函数。当调用fwrite函数时,它会将指定数量的数据从内存缓冲区写入到文件中。fwrite函数是一个阻塞函数,它会等待数据写入完成后才返回。如果写入过程中发生错误,fwrite函数会返回一个小于预期写入数量的值。
#include <stdio.h>
int main() {
FILE *file = fopen("data.txt", "w");
if (file == NULL) {
printf("Failed to open file.\n");
return 1;
char data[] = "Hello, World!";
size_t size = sizeof(data[0]);
size_t count = sizeof(data) / size;
size_t written = fwrite(data, size, count, file);
if (written != count) {
printf("Failed to write data.\n");
} else {
printf("Data written successfully.\n");
fclose(file);
return 0;