日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

fread和fwrite函数功能

發布時間:2024/4/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 fread和fwrite函数功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

fread和fwrite函數功能

? 用來讀寫一個數據塊。

一般調用形式

? fread(buffer,size,count,fp);

? fwrite(buffer,size,count,fp);

說明

? (1)buffer:是一個指針,對fread來說,它是讀入數據的存放地址。對fwrite來說,是要輸出數據的地址。

? (2)size:要讀寫的字節數;

? (3)count:要進行讀寫多少個size字節的數據項;

? (4)fp:文件型指針。

注意:1 完成次寫操(fwrite())作后必須關閉流(fclose());

?????????? 2 完成一次讀操作(fread())后,如果沒有關閉流(fclose()),則指針(FILE * fp)自動向后移動前一次讀寫的長度,不關閉流繼續下一次讀操作則接著上次的輸出繼續輸出;

?????????? 3 fprintf() : 按格式輸入到流,其原型是int fprintf(FILE *stream, const char *format[, argument, ...]);其用法和printf()相同,不過不是寫到控制臺,而是寫到流罷了。注意的是返回值為此次操作寫入到文件的字節數。如int c = fprintf(fp, "%s %s %d %f", str1,str2, a, b) ;str1:10字節;str2: 10字節;a:2字節;b:8字節,c為33,因為寫入時不同的數據間自動加入一個空格。

文件使用之后一定要關閉,否則將不能正確顯示內容.fwrite:讀入兩個學生信息然后用fwrite存入文件

fread:用fread從文件中讀出學生信息。

fwrite.c

#include <stdio.h>
#define SIZE 2
struct student_type
{
char name[10];
int num;
int age;
char addr[10];
}stud[SIZE];
void save()
{
FILE *fp;
int i;
if((fp=fopen("stu_list","wb"))==NULL)
{
? printf("cant open the file");
? exit(0);
}
for(i=0;i<SIZE;i++)
{
?? if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
??? printf("file write error\n");
}
fclose(fp);
}
main()
{
int i;
for(i=0;i<SIZE;i++)
{
?? scanf("%s%d%d%s",&stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);
?? save();
}
for(i=0;i<SIZE;i++)
{
?? printf("%s,%d,%d",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
}

fread.c

#include <stdio.h>
#define SIZE 2
struct student_type
{
char name[10];
int num;
int age;
char addr[10];
}stud[SIZE];
void read()
{
FILE *fp;
int i;
if((fp=fopen("stu_list","rb"))==NULL)
{
? printf("cant open the file");
? exit(0);
}
for(i=0;i<SIZE;i++)
{
?? if(fread(&stud[i],sizeof(struct student_type),1,fp)!=1)
??? printf("file write error\n");
}
fclose(fp);
}
main()
{

int i;
read();
for(i=0;i<SIZE;i++)
{
?? printf("%s,%d,%d,%s",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
?? printf("\n");
}
}

轉載于:https://www.cnblogs.com/to-creat/p/5643900.html

總結

以上是生活随笔為你收集整理的fread和fwrite函数功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。