Linux的fwrite函数

函数原型:

向文件fp中写入writeBuff里面的内容

int fwrite(void*bufferintsizeintcountFILE*fp)

/*
* @description : 对已打开的流进行写入数据块
* @param ‐ ptr :指向 数据块的指针
* @param ‐ size :指定写入的每个数据项的字节数,如调用 sizeof(char)
* @param ‐ nmemb : 指定写入的数据项的个数
* @param ‐ stream :要写入的文件流
* @return : fwrite函数返回实际写入的数据项的个数
*/
fread() ── fp 所指向文件的当前位置开始,一次读入 size 个字节,重复 count 次,并将读入的数据存放到从buffer 开始的内存中; buffer 是存放读入数据的起始地址(即存放何处)。
fwrite() ── buffer 开始,一次输出 size 个字节,重复 count 次,并将输出的数据存放到 fp 所指向的文件 中。buffer 是要输出数据在 内存中的起始地址(即从何处开始输出)。
一般用于二进制文件的处理。
#include<stdio.h>
#include<string.h>

int main()
{
FILE *fp = NULL;
int nRet = 0 ;
char readBuff[12];
char *writeBuff="hello world";/*在指针数组里面存放hello world*/
memset(readBuff,0,12);
fp =fopen("ll","r+");/*r+为对文件可读可写,且从光标后面开始写*/
if (fp == NULL)
{
printf("open failed!\n");
return -1;

}

printf("open success!\n");


nRet =fread(readBuff,4,2,fp);
if(nRet<=0)
{
printf("readBuff failed!");
return -3;

}
printf("readBuff is %s",readBuff);

nRet = fwrite(writeBuff,4,1,fp);/*从writeBuff里面取四个字节,数量为1写入到fp文件里面取*/
if(nRet<=0)
{
printf("write  failed!");
return -4;

}






nRet = fclose(fp);
if(nRet)
{

printf("close failed!\n");
return -2;

}
printf("close success!\n");
return 0;


}

ll文件夹数据为123456789

cat ll命令为显示ll文件里面的内容。

上面代码的流程:

首先从ll文件夹里面读取8个字节到readBuff,所以readBuff里面的内容为12345678

然后将writeBuff里面的内容hello world写出4个字节到文件ll.

因为上次读完ll的8个字节后,光标位于8因此从8开始写入4个字节为12345678hell

#include<stdio.h>
#include<string.h>

int main()
{
FILE *fp = NULL;
int nRet = 0 ;
char readBuff[12];
char *writeBuff="hello world";
memset(readBuff,0,12);
fp =fopen("ll","r+");
if (fp == NULL)
{
printf("open failed!\n");
return -1;

}

printf("open success!\n");


nRet =fread(readBuff,4,1,fp);
if(nRet<=0)
{
printf("readBuff failed!");
return -3;

}
printf("readBuff is %s",readBuff);

nRet = fwrite(writeBuff,4,1,fp);
if(nRet<=0)
{
printf("write  failed!");
return -4;

}






nRet = fclose(fp);
if(nRet)
{

printf("close failed!\n");
return -2;

}
printf("close success!\n");
return 0;


}

如果第一次只读4个字节,ll内容为12345678hell

则读完后readBuff为1234

此时光标在4后面,在继续读入4个字节,ll内容为1234hellhell

#include<stdio.h>
#include<string.h>

int main()
{
FILE *fp = NULL;
int nRet = 0 ;
char readBuff[12];
char *writeBuff="hello world";
memset(readBuff,0,12);
fp =fopen("ll","r+");
if (fp == NULL)
{
printf("open failed!\n");
return -1;

}

printf("open success!\n");


nRet =fread(readBuff,4,1,fp);
if(nRet<=0)
{
printf("readBuff failed!");
return -3;

}
printf("readBuff is %s",readBuff);

nRet = fwrite(writeBuff,4,2,fp);
if(nRet<=0)
{
printf("write  failed!");
return -4;

}






nRet = fclose(fp);
if(nRet)
{

printf("close failed!\n");
return -2;

}
printf("close success!\n");
return 0;


}

如果第一次只读4个字节,ll内容为1234hellhell

则读完后readBuff为1234

此时光标在4后面,在继续读入8个字节,ll内容为1234hello wo

说明r+的写入是从光标后面写入,会覆盖写入的那几位的内容,写入长度如果小于光标后面的长度,则写入完成后,多余的长度里面的内容不变。如果写入大于后面的长度,则后面所有内容均会被覆盖

### `fwrite` 函数详解及其用法 #### 定义与功能 `fwrite` 是 C 和 C++ 编程语言中的标准库函数之一,主要用于将数据写入文件流。它可以高效地批量写入指定数量的数据项至目标文件中[^5]。 #### 原型声明 ```c size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); ``` - **ptr**: 数据源的指针,指向要写入的第一个元素。 - **size**: 单个数据项的大小(以字节为单位)。 - **nmemb**: 要写入的数据项数目。 - **stream**: 指定的目标文件流对象。 此函数成功执行后返回实际写入的数量;如果遇到错误,则返回值小于请求写的成员数。当返回值不等于参数 `nmemb` 时,可能意味着发生了 I/O 错误或者到达了文件结尾[^6]。 #### 使用示例 下面是一个简单例子演示如何运用 `fwrite` 将一组整数保存到磁盘上的二进制文件里: ```c #include <stdio.h> int main(){ int data[] = {10, 20, 30}; FILE *file; file = fopen("output.bin", "wb"); if(file == NULL){ perror("Error opening file"); return(-1); } // Write three integers into binary file 'output.bin' size_t result = fwrite(data, sizeof(int), 3, file); if(result != 3){ fprintf(stderr,"Failed to write complete array to file\n"); }else{ puts("Data written successfully!"); } fclose(file); return(0); } ``` 在此案例中,我们首先定义了一组三个整数作为待写入的数据集。接着打开名为 `"output.bin"` 的新文件用于写入 (`"wb"` 表示创建一个新的二进制文件并准备写入),随后调用 `fwrite` 方法一次性写出全部三项记录。最后记得关闭文件结束操作[^7]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值