`fwrite` 函数是 C 语言中用于向文件写入数据的函数。它属于标准库函数,通常在 `stdio.h` 头文件中声明。`fwrite` 函数的返回值表示实际写入文件的字节数。
`fwrite` 函数的原型如下:
```c 
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); 
```
参数说明:
- `ptr`:指向要写入文件的数据的指针。 
- `size`:每个元素的大小(以字节为单位)。 
- `nmemb`:要写入文件的数据元素数量。 
- `stream`:文件指针,指向要写入的文件。
返回值:实际写入文件的字节数,如果写入失败或到达文件末尾,返回 `0`。
以下是一个简单的示例,向文件 "" 写入一行文本:
```c 
#include <stdio.h>
int main() { 
    FILE *file = fopen("", "w"); 
    if (file == NULL) { 
        printf("无法打开文件!\n"); 
        return 1; 
fopen函数失败    }
    const char *text = "Hello, World!"; 
    size_t written_bytes = fwrite(text, 1, strlen(text), file);
    if (written_bytes == 0) { 
        printf("写入失败!\n"); 
    } else { 
        printf("成功写入 %zu 字节数据!\n", written_bytes); 
    }
    fclose(file); 
    return 0; 
}
```
在这个示例中,我们尝试将字符串 "Hello, World!" 写入文件 ""。如果写入成功,`fwrite` 函数将返回实际写入的字节数(本例中为 13,包括换行符)。如果写入失败,返回值将为 0。