在c语言中,怎么把数据连续的输入到文件中去?

如题所述

#include "stdio.h"
#define SIZE 4
struct student_type
{
char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];
void save()
{
FILE *fp;
int i;
if((fp=fopen("test.txt","wt"))==NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i<SIZE;i++)
fwrite(&stud[i],sizeof(struct student_type),1,fp);
fclose(fp);
}
void main()
{
int i;
for(i=0;i<SIZE;i++)
{
printf("第%d个学生的信息:\n",i+1);
scanf("%s",stud[i].name);
scanf("%d",&stud[i].num);
scanf("%d",&stud[i].age);
scanf("%s",stud[i].addr);
}
save();

}追问

那怎么从文件中读出来然后按名字或学号来查找到想要的信息并输出到屏幕上呢?

追答

void read()
{
FILE *fp;
int i;
if((fp=fopen("test.txt","rt"))==NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i=0)
printf("该学生的信息:学号%ld 姓名%s 年龄%d 地址%s\n",num,stud[j].name,stud[j].age,stud[j].addr);
else
printf("该班没有这个学生\n");

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-01-06
printf("连续输入,字符 0 结束:\\n"); while (scanf("%s", word)!=("输入删除的单词\\n"); scanf("%s", word); deltree(root, word);
第2个回答  2012-01-06
文件指针
int main()
{
FILE *fp=NULL;
fp=fopen("C:\\stp.TXT","r");

if(fp==NULL){
printf(" 文件不存在 \n");
return -1;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网