在C语言中如何将输入结构体中的数组信息存为文件

#include #include#include #include #include#include#define N 10 struct stock /* 股票结构体 */{ char stock_code[20]; char stock_name[20]; int stocknum; float updownprice; int nowprice; int profit ; float totalvalue; int refund; float totalprofit; struct stock *p; };这一个结构体,再输入信息后在EXE中显示后,如何将其中的信息再存为TXT文件!!!
可以将结构体的数据进行保存,例如
struct a
{
int a1;

int a2;

};
对于此数据闷哪碧结构,可以使用fwrite函数进行保存,其原型定义如下:
size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
根据此函数,可以用如下的方法进行蚂举保存缓旅:
struct a;
a.a1 = 0;
a.a2 = 2;
fwrite(&a,sizeof(a),1,fp);
fopen 一个文件 然后用 fwrite 写到一个迟做文件就行了!纳旦槐
struct stock My;
FILE *fp=NULL;
fp=fopen("C:\\test.txt"洞友,"w");
if(fp !=NULL)
{
fwrite(&My,sizeof(struct stock),1,fp);
fclose(fp);
}
在C语言中如何将输入结构体中的数组信息存为文件
← 上一篇: 下一篇: →