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

#include<stdio.h> #include<iostream.h>#include<string.h> #include<conio.h> #include<stdlib.h>#include<fstream.h>#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);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-26
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);
}本回答被网友采纳