生活随笔
收集整理的這篇文章主要介紹了
百度百科中关于fwrite的用法说明
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
注意:這個(gè)函數(shù)以二進(jìn)制形式對(duì)文件進(jìn)行操作,不局限于文本文件
返回值:返回實(shí)際寫(xiě)入的數(shù)據(jù)塊數(shù)目
(1)buffer:是一個(gè)指針,對(duì)fwrite來(lái)說(shuō),是要獲取數(shù)據(jù)的地址;
(2)size:要寫(xiě)入內(nèi)容的單字節(jié)數(shù);
(3)count:要進(jìn)行寫(xiě)入size字節(jié)的數(shù)據(jù)項(xiàng)的個(gè)數(shù);
(4)stream:目標(biāo)文件指針;
(5)返回實(shí)際寫(xiě)入的數(shù)據(jù)項(xiàng)個(gè)數(shù)count。
說(shuō)明:寫(xiě)入到文件的哪里? 這個(gè)與文件的打開(kāi)模式有關(guān),如果是w+,則是從file pointer指向的地址開(kāi)始寫(xiě),替換掉之后的內(nèi)容,文件的長(zhǎng)度可以不變,stream的位置移動(dòng)count個(gè)數(shù);如果是a+,則從文件的末尾開(kāi)始添加,文件長(zhǎng)度加大。
fseek對(duì)此函數(shù)有作用,但是fwrite
[1]函數(shù)寫(xiě)到用戶空間緩沖區(qū),并未同步到文件中,所以修改后要將內(nèi)存與文件同步可以用fflush(FILE *fp)函數(shù)同步。
4程序示例編輯
示例一:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdio.h> struct mystruct { ????int i; ????char cha; }; int main(void) { ????FILE *stream; ????struct mystruct s; ????if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */ ????{ ????????fprintf(stderr, "Cannot open output file.\n"); ????????return 1; ????} ????s.i = 0; ????s.cha = 'A'; ????fwrite(&s, sizeof(s), 1, stream); /* 寫(xiě)的struct文件*/ ????fclose(stream); /*關(guān)閉文件*/ ????return 0; } |
示例二:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include<stdio.h> #define SIZE 1 typedef struct { ????char name[10]; ????int num; ????int age; ????char addr[15]; ????}student; student stu[SIZE]; void save() { ????FILE *fp; ????int i; ????if((fp=fopen("dat.txt","w"))==NULL) ????{ ????????printf("無(wú)法打開(kāi)此文件!\n"); ????????return; ????} ????for(i=0;i<SIZE;i++) ????if(fwrite(&stu[i], sizeof(student), 1, fp) != 1) ????printf("文件寫(xiě)入錯(cuò)誤。!\n"); ????fclose(fp); } void main() { ????int i; ????for(i=0;i<SIZE;i++) ????????scanf("%s%d%d%s",&stu[i].name,&stu[i].num,&stu[i].age,&stu[i].addr); ????save(); } |
示例三:
| 1 2 3 4 5 6 7 8 9 10 11 | /* fwrite example : write buffer */ #include <stdio.h> int main () { ????FILE * pFile; ????char buffer[] = { 'x' , 'y' , 'z' }; ????pFile = fopen ( "myfile.bin" , "wb" ); ????fwrite (buffer , sizeof(buffer), 1 , pFile ); ????fclose (pFile); ????return 0; } |
稱為myfile.bin的一個(gè)文件被創(chuàng)建并存儲(chǔ)到它的緩沖區(qū)的內(nèi)容。為了簡(jiǎn)單起見(jiàn),該緩沖區(qū)包含Char元素,但它可以包含任何其他類型。.
sizeof(buffer)字節(jié)數(shù)組的長(zhǎng)度(在這種情況下,它是三個(gè),因?yàn)閿?shù)組有三個(gè)元素,每次一個(gè)字節(jié))。
示例四:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //程序示例 fwrite fread fseek #include <stdio.h> int main () { ????FILE *fp; ????char msg[] = "file content"; ????char buf[20]; ????fp = fopen("d:\\a\\a.txt","w+"); ????if (NULL == fp) ????{ ????????printf("The file doesn't exist!\n"); ????????return -1; ????} ????fwrite(msg,strlen(msg),1,fp);//把字符串內(nèi)容寫(xiě)入到文件 ????fseek(fp,0,SEEK_SET);//定位文件指針到文件開(kāi)始位置 ????fread(buf,strlen(msg),1,fp);//把文件內(nèi)容讀入到緩存 ????buf[strlen(msg)] = '\0';//刪除緩存內(nèi)多余的空間 ????printf("buf = %s\n",buf); ????printf("strlen(buf) = %d\n",strlen(buf)); ????return 0; } |
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的百度百科中关于fwrite的用法说明的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。