C fwrite

功能:向文件读入写入一个数据块。

用法:fwrite(const void *buffer,size_t size,size_t count,FILE
*stream);

(1)buffer:是一个指针,对fwrite 来说,是要输出数据的地址。

(2)size:要写入内容的单字节数;

(3)count:要进行写入size字节的数据项的个数;

(4)stream:目标文件指针。

说明:写入到文件的哪里与文件的打开模式有关,如果是r+,则是从file pointer
指向的地址开始写,替换掉之后的内容,文件的长度可以不变,stream的位置移动count个数。如果是a+,则从文件的末尾开始添加,文件长度加大,而且是fseek函数对此函数没有作用。

demo1:

[cpp] view plaincopy

  1. #include <stdio.h>

  2. #include <process.h>

  3. typedef struct

  4. {

  5. int i;

  6. char ch;

  7. }mystruct;

  8. int main()

  9. {

  10. FILE *stream;

  11. mystruct s;

  12. /*wb只写打开或新建一个二进制文件;只允许写数据。*/

  13. if ((stream=fopen("test.$$$","wb"))==NULL)

  14. {

  15. fprintf(stderr,"cannot open output file.\n");

  16. return 1;

  17. }

  18. s.i=0;

  19. s.ch=‘A‘;

  20. fwrite(&s,sizeof(s),1,stream);

  21. fclose(stream);

  22. stream=NULL;

  23. system("pause");

  24. return 0;

  25. }


demo2:

[cpp] view plaincopy

  1. #include <stdio.h>

  2. int main()

  3. {

  4. FILE *pFile=NULL;

  5. char buffer[]={‘x‘,‘y‘,‘z‘};

  6. pFile=fopen("myfile.bin","wb");

  7. fwrite(buffer,sizeof(buffer),1,pFile);

  8. fclose(pFile);

  9. system("pause");

  10. return 0;

  11. }

demo3:

[cpp] view plaincopy

  1. #include <stdio.h>

  2. #include <process.h>

  3. int main()

  4. {

  5. FILE *fp=NULL;

  6. char msg[]="file content";

  7. char buf[20];

  8. fp=fopen("c:\\a.txt","w+");    //二级目录会不成功

  9. if (NULL==fp)

  10. {

  11. printf("The file doesn‘t exist!\n");

  12. getchar();

  13. getchar();

  14. return -1;

  15. }

  16. fwrite(msg,strlen(msg),1,fp);   //把字符串内容写入到文件

  17. fseek(fp,0,SEEK_SET);           //定位文件指针到文件首位置

  18. fread(buf,strlen(msg),1,fp);    //把文件读入到缓存

  19. buf[strlen(msg)]=‘\0‘;          //删除缓存内多余空间

  20. printf("buf=%s\n",buf);

  21. printf("strlen(buf) = %d\n",strlen(buf));

  22. system("pause");

  23. return 0;

  24. }


C fwrite,布布扣,bubuko.com

时间: 2024-08-26 02:41:11

C fwrite的相关文章

文件流:&quot;fopen&quot;,&quot;fclose&quot;,“ftell”&quot;fseek&quot;,&quot;fgets&quot;,&quot;fprintf&quot; ,“feof”,&quot;fwrite&quot;,&quot;fread&quot;

char const* filename="D:/hello.txt"; "fopen", FILE *fp=fopen(char const *name,char const mode); e.g:FILE *fp = fopen(filename,"wb"); 打开文件流,name为要打开文件的路径,如这里的filename:mode 为对文件的操作模式,通常使用:"wb"(写操作),"rb"(读操作)

fread(),fwrite() 读/写流

C 库函数 - fread() 描述 C 库函数 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) 从给定流 stream 读取数据到 ptr 所指向的数组中. 声明 下面是 fread() 函数的声明. size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) 参数 ptr -- 这是指向带有最小尺寸 size*nmemb 字节的内存块的指针.

fscanf函数和fprintf函数、fgets函数和fputs函数、fread函数和fwrite函数

1. fscanf 函数和 fprintf 函数 1.1 fscanf 函数 fscanf 函数只能从文本文件中按格式输入.fscanf 函数和 scanf 函数相似,只是输入的对象是磁盘上文本文件的数据.函数的调用形式如下: fscanf( 文件指针,格式控制字符串,输入项表 ); 例如,若文件指针 fp 指向一个已打开的文本文件,a.b 分别为整型变量,则以下语句从 fp 所指的文件中读入两个整数放入变量 a 和 b 中: fscanf( fp, "%d%d", &a, &

关于二进制文件fread、fwrite函数使用读写

环境:vs2013 语言:C语言 时间:2015年3月10日 功能:实现二进制文件的读写实例 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #define FILENAME "d:/studentInfo" #define COUNT 5 typedef struct { char name[10]; short Math; short Chinese; short

8.5 文件IO fgets fputs fgetc fwrite fread

fopen函数mode模式: w+不是追加写 是多了一个读权限 文件指针+1没有意义 拷贝一个文件: fgets     fputs   (fgetc同理) int main() { FILE *fp, *fpcp; fp = fopen("yesteday_once_more.txt", "r"); fpcp = fopen("a", "w"); char buf[1024]; //一行给他1024个字节 尽量够用 whil

用fread和fwrite实现文件复制操作

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,char **argv) { FILE *fp_src,*fp_des; char buf[128]; int num; if(argc!=3) { printf("the format must be: cp_example file_src file_des\n"); exit(EXIT_FAI

fread和fwrite同时对一个文件读写

这几天看到了fopen的参数设置.中文的那些真的是不能帮助精确理解.在网上发现了英文的,特附上: FILE *fopen(const char *filename, const char *mode)fopen opens the named file, and returns a stream, or NULL if the attempt fails. Legal values for mode include:"r"open text file for reading"

C语言:用二进制方式向文件读写一组数据(fread、fwrite)

#include<stdio.h> #define SIZE 10 struct student { char name[10]; int num; int age; char addr[15]; }stu[SIZE]; //保存数据(fwrite) void save() { FILE *fp; fp = fopen("stu.dat","wb"); if(fp==NULL) { printf("file can not open!\n&qu

文件内容操作篇clearerr fclose fdopen feof fflush fgetc fgets fileno fopen fputc fputs fread freopen fseek ftell fwrite getc getchar gets

clearerr(清除文件流的错误旗标) 相关函数 feof 表头文件 #include<stdio.h> 定义函数 void clearerr(FILE * stream); 函数说明 clearerr()清除参数stream指定的文件流所使用的错误旗标. 返回值 fclose(关闭文件) 相关函数 close,fflush,fopen,setbuf 表头文件 #include<stdio.h> 定义函数 int fclose(FILE * stream); 函数说明 fclos

Linux 打开文件并写入一段字符串,同时读出对应文件的信息--fopen()/fwrite()/fread()

Linux 打开文件并写入一段字符串,同时读出对应文件的信息 在Linux中,采用C语言,使用fopen()函数打开一个文件(若不存在文件,则新建一个 如 data/test.dat),同时采用 fwrite()向其中写入相应的字符串,每次输入写入都换行,最后采用fread()读出相应的信息. 相应的代码如下:Test.c /* Test.c :create a file : Write a string to the file */ #include <stdio.h> #include &