Ⅰ 求助,c语言从TXT读取数据存入结构体中,被困扰了好久。。
#include<stdio.h>
structscore
{
intid;
charname[50];
floatchinese;
floatmath;
floatenglish;
floatave;
floatsum;
};
#definen16
intmain()
{
inti,j;
structscorestu[n+1];
FILE*fp;
fp=fopen("E:\stuScores.txt","rt");
if(fp==NULL)
{
printf("无法打开文件! ");
return0;
}
for(i=0;i<16;i++)
{
fscanf(fp,"%d",&stu[i].id);
fscanf(fp,"%s",stu[i].name);///-----------
fscanf(fp,"%f",&stu[i].chinese);
fscanf(fp,"%f",&stu[i].math);
fscanf(fp,"%f",&stu[i].english);
stu[i].sum=stu[i].chinese+stu[i].id+stu[i].english;
stu[i].ave=stu[i].sum/3;
if(feof(fp))
break;
}
fclose(fp);
j=i;
for(i=0;i<j;i++)
{
printf("%d ",stu[i].id);
printf("%s ",stu[i].name);
printf("%f ",stu[i].chinese);
printf("%f ",stu[i].math);
printf("%f ",stu[i].english);
printf("%f %f ", stu[i].sum, stu[i].ave);
}
return0;
}
Ⅱ c语言 将txt文件导入结构体数组
txt格式
20170043556875 张某 机械工程1414
12334545654677 李某 有机化学1313
code:
#include<stdio.h>
#defineRECORDNUMBER100
typedefstructSTUDENT{
charname[50];
unsignedlongid;
charclass[50];
}stu;
intmain(){
studata[RECORDNUMBER];
FILE*fp=fopen("a.txt","r");
if(!fp)return-1;
inti=0;
while(fscanf(fp,"%ld%s%s",&data[i].id,data[i].name,data[i].class)!=EOF){
printf("%ld,%s,%s ",data[i].id,data[i].name,data[i].class);
i++;
}
fclose(fp);
}
Ⅲ C语言如何将txt文件中的数据存入结构体中
因为结构体它里面有多种数据, 所以如果一个一个保存肯定是麻烦的. 所以, 如果保存的文件不是给用户看的, 而是只为程序所用, 那么直接用二进制方式打开文件, 然后用fwrite把结构的二进制数据写入内存
#include<stdio.h>
structdat
{
inti;charch;doubled;
};
intmain()
{
FILE*p=fopen("data.txt","w");
if(!p)
return0;
fwrite(&p,sizeof(p),1,p);
fclose(p);
}
Ⅳ 如何将txt文件中的学生信息读取到结构体数组中,C语言
FILE*fp;
printf("请输入文件名,路径:");//打开txt文件
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{
fprintf(stderr,"%s文件打开失败! ",filename);
exit(0);}
for(inti=0;i<12;i++)//循环读取
{
fscanf(fp,"%s%s%f",StuInfo[i].ID,StuInfo[i].name,StuInfo[i].score);
printf("%s %s %f ",StuInfo[i].ID,StuInfo[i].name,StuInfo[i].score);
}
Ⅳ 如何用C语言读取txt文件中的数据到结构体数组中
C语言使用FILE指针来读写文件,
比如: FILE *fp = fopen("D:\\Student.txt","r");
打开成功后 可以逐行读取文本,然后进行处理赋值就可以了。
Ⅵ c++读取txt文件到结构体
需要用到fstream头文件
用ifstream进行读取
读取到结构体里面主要是每次读取对应结构体变量长度的数据到该结构体里面
具体参考下面demo
strcutstockType
{
stringpersonAddress;
stringpersonCity;
stringpersonState;
intpersonZIP;
};
voidaddressType::getData()
{
ifstreaminfile;
intindex;
stringinputFile;
stockTypeaddressTypeList[NO_OF_PERSON];
cout<<"Enterthefilepath:";
cin>>inputFile;//输入txt文件路径
cout<<endl;
infile(inputFile.c_str());//infile按照指定路径读取txt文件
for(index=0;index<NO_OF_PERSON;index++)//读取txt文件至struct
{
infile>>addressTypeList[index].personAddress;
infile>>addressTypeList[index].personCity;
infile>>addressTypeList[index].personState;
infile>>addressTypeList[index].personZIP;
}
}
Ⅶ c语言怎么把txt格式的文件读到结构体里
1.你得先弄会流文件的读取http://blog.csdn.net/sky101010ws/article/details/6744062 这里是流文件的相关函数
2.读取流文件之后,获取的文件中的字符串信息也就是a a a a a 1 1 1 1 b b b b b 2 2 2 2 2 c c c c c 3 3 3 3。然后判断字符串中的空格和换行符,截取其中的a 1 b 2 c 3等字符(都是char型),接着判断1 2 3的ascii码范围,将其转换为整形(利用函数 int atoi(const char *nptr);)。当然,如果你TXT文件中的字符信息都是固定格式的,那就可以省略1 2 3 整形信息的判断,直接将其转换为整形。
3.获取文件中需要的信息a 1 b 2 c 3的同时,将其输入结构体中就可以了。
Ⅷ c语言怎么从txt文档中读取内容到一个结构体当中啊
设定文件指针,然后用fget,fgets之类的读取数据,以下是我很久以前做练习编的有关文件读取的程序,你可以参考下
#include<stdio.h>
#include<windows.h>
struct stu
{
char name[10];
int num;
char sex;
int age;
}boya[2],boyb[2],*p,*q;
void main() //argv[0]保存的是文件名,argc为参数个数,即argv[]数组元素个数
{
FILE *fp;
char ch;
if((fp=fopen("yh.txt","rt"))==NULL)
{
printf("文件打开失败!\n");
exit(1);
}
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
printf("\n");
fclose(fp);
printf("\n");
FILE *fp2;
if((fp2=fopen("yh2.txt","wt+"))==NULL)
{
printf("打开文件失败!\n");
exit(1);
}
ch=getchar();
while(ch!=EOF)
{
fputc(ch,fp2);
ch=getchar();
}
rewind(fp2);
ch=fgetc(fp2);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp2);
}
printf("\n");
fclose(fp2);
if((fp=fopen("yh2.txt","at+"))==NULL)
{
printf("文件打开失败!\n");
exit(1);
}
ch=getchar();
while(ch!=EOF)
{
fputc(ch,fp);
ch=getchar();
}
printf("\n");
fclose(fp);
p=boya;
q=boyb;
if((fp=fopen("stu-yh.txt","wb+"))==NULL)
{
printf("无法打开文件!\n");
exit(1);
}
printf("输入学生名字,学号,性别\n");
for(int i=0;i<2;i++,p++)
{
scanf("%s%d%c%d",&p->name,&p->num,&p->sex,&p->age);
getchar();
}
p=boya;
fwrite(p,sizeof(struct stu),2,fp);
fclose(fp);
}
然后还有一个网络答题编写的一个学生管理系统,你也看下
#include<stdio.h>
#include<windows.h>
#include<string.h>
typedef struct student
{
char name[12];
char sex[4];
char birth[40];
int clas;
int stunum;
int age;
int chinese;
int math;
int english;
int aver;
struct student *next;
}stu,*stur;
//读取信息并创建链表
void CreateList(stur &L)
{
FILE *fp;
stu *p;
L=(stur)malloc(sizeof(stu));
L->next=NULL;
if((fp=fopen("yh.txt","r"))==NULL)
{
printf("打开文件失败!\n");
exit(1);
}
while(!feof(fp))
{
p=(stur)malloc(sizeof(stu));
fscanf(fp,"%s",p->name);
fscanf(fp,"%s",p->sex);
fscanf(fp,"%s",p->birth);
fscanf(fp,"%d",&p->clas);
fscanf(fp,"%d",&p->stunum);
fscanf(fp,"%d",&p->age);
fscanf(fp,"%d",&p->chinese);
fscanf(fp,"%d",&p->math);
fscanf(fp,"%d",&p->english);
p->aver=(p->chinese+p->english+p->math)/3;
p->next=L->next;
L->next=p;
}
printf("读入数据成功!\n");
system("pause");
}
void PList(stur &L) //打印
{
system("mode con cols=90 lines=50"); //扩大窗口
stu *p;
p=L->next;
if(p->age<0)
{
printf("没有数据!\n");
return;
}
printf(" 姓名 | 性别 | 生日 | 班级 | 学号 | 年龄 | 语文 | 数学 | 英语 |\n");
while(p)
{
printf("%6s |%6s |%9s|%6d |%6d |%6d |%6d |%6d |%6d |\n",p->name,p->sex,p->birth,p->clas,p->stunum,p->age,p->chinese,p->math,p->english);
p=p->next;
}
printf("\n");
}
void ListInsert(stur &L) //插入元素并写入文件
{
system("mode con cols=100 lines=10"); //扩大窗口
stur p,s;
FILE *fp;
p=L;
printf("执行添加操作,输入姓名,性别,生日,班级,学号,年龄,语文成绩,数学成绩,英语成绩\n(如:yh1 女 16 2000.1.12 110203 110203056 100 100 90):\n");
s=(stur)malloc(sizeof(stu));
scanf("%s",s->name);
getchar();
scanf("%s",s->sex);
getchar();
scanf("%s %d %d %d %d %d %d",s->birth,&s->clas,&s->stunum,&s->age,&s->chinese,&s->math,&s->english);
s->aver=(s->chinese+s->english+s->math)/3;
if((fp=fopen("yh.txt","at+"))==NULL)
{
printf("文件打开失败!\n");
exit(1);
}
fprintf(fp,"%s %s %s %d %d %d %d %d %d",s->name,s->sex,s->birth,s->clas,s->stunum,s->age,s->chinese,s->math,s->english);
fclose(fp);
s->next=p->next;
p->next=s;
printf("已更新数据!\n");
}
void CList(stur &L) //排序
{
stu *p,*temp1,*temp2,*temp3;
int flag;
p=L;
while(1)
{
flag=0;
for(p=L;p->next->next!=NULL;p=p->next)
{
if(p->next->aver>p->next->next->aver)
{
temp1=p->next;
temp2=p->next->next;
temp3=p->next->next->next;
p->next=temp2;
temp2->next=temp1;
temp1->next=temp3;
}
}
if(flag==0)
{
printf("排序成功!\n");
return;
}
}
}
void searchL(stur &L) //单向链表查询只能一个一个排查
{
int key;
printf("输入学号!\n");
scanf("%d",&key);
stu *p;
p=L;
while(p)
{
if(p->stunum==key)
{
system("mode con cols=100 lines=10"); //扩大窗口
printf("查询成功!\n");
printf(" 姓名 | 性别 | 生日 | 班级 | 学号 | 年龄 | 语文 | 数学 | 英语 |\n");
printf("%6s |%6s |%9s|%6d |%6d |%6d |%6d |%6d |%6d |\n",p->name,p->sex,p->birth,p->clas,p->stunum,p->age,p->chinese,p->math,p->english);
return;
}
p=p->next;
}
printf("查无此人!\n");
return;
}
void main()
{
char ch;
stur L;
printf("读取中,请稍候……\n"); //如果读入数据不是非常多就删除它吧
CreateList(L);
while(1)
{
system("mode con cols=60 lines=10"); //缩小窗口
printf("***********************功能导航主页面***********************\n");
printf("1.添加学员信息\n2.察看学员信息\n3.按学员总体成绩排序\n4.按学号查询\n5.退出\n");
scanf("%c",&ch);
switch(ch)
{
case '1':
ListInsert(L);
getchar();
system("pause");
system("cls");
break;
case '2':
PList(L);
getchar();
system("pause");
system("cls");
break;
case '3':
CList(L);
getchar();
system("pause");
system("cls");
break;
case '4':
system("cls");
searchL(L);
getchar();
system("pause");
system("cls");
break;
case '5':
system("cls");
printf("已退出,期待你的下次使用!\n");
return;
default:
printf("错误的指令!\n");
getchar();
system("cls");
system("pause");
break;
}
}
}
Ⅸ 关于c语言读取文本数据到结构体的问题
用错函数了。fread的第一个参数是数组,不是结构体。
用fscanf的格式化可以对应每一项数据对应的读入结构体的每一个成员中。
我只给txt中写了三行数据,测试一下效果。
#include<stdio.h>
#defineN10
structplan{
charID[6];
charoutset[10];
chardestination[10];
charsech[6];
intseatnum;
intpnum;
}plandata[N];
main(){
FILE*fp;
if((fp=fopen("plan.txt","r"))==NULL){
printf("无法打开文件。 ");
return;
}
printf(" 航班号 出发地 终点 时间 座位数 剩余票数 ");
inti=0;
while(!feof(fp)){
fscanf(fp,"%s %s %s %s %d %d",plandata[i].ID,plandata[i].outset,plandata[i].destination,plandata[i].sech,&plandata[i].seatnum,&plandata[i].pnum);
printf("%s %s %s %s %d %d ",plandata[i].ID,plandata[i].outset,plandata[i].destination,plandata[i].sech,plandata[i].seatnum,plandata[i].pnum);
i++;
}
fclose(fp);
}
运行结果如图
Ⅹ C语言怎样将.txt文件中的数据写入到结构体中去
txt文件中的数据写入到结构体中去的源代码如下:
#include<stdio.h>
#include <string.h>
//可以退出的头文件
#include <stdlib.h>
//结构体的长度
#define DATALEN 15
//函数声明
//定义结构数组
struct wordUnit{
int id; //id
char word[10]; //词语
char depId[10]; //依存词语的id
char pos[10]; //词性
char depRel[10]; //依存目标的关系
};
int main(){
FILE *data;//要读取的文件指针
int i=0;//结构题数组移动
struct wordUnit words[DATALEN];
if((data=fopen("data3.txt","r"))==NULL){
printf("Can not open file ");
return 0;
}
while(!feof(data)){
//原txt文档的数据之间是以空格隔开的
}
fclose(data);
for(int j=0;j<i;j++){
}
return 0;
}
(10)c语言读取tst文件到结构体中扩展阅读
1、使用关键字struct,它表示接下来是一个结构体。
2、后面是一个可选的标志(book),它是用来引用该结构体的快速标记。