當前位置:首頁 » 編程語言 » c語言讀取tst文件到結構體中
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言讀取tst文件到結構體中

發布時間: 2023-02-21 10:25:17

Ⅰ 求助,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文件到結構體

  1. 需要用到fstream頭文件

  2. 用ifstream進行讀取

  3. 讀取到結構體裡面主要是每次讀取對應結構體變數長度的數據到該結構體裡面

具體參考下面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),它是用來引用該結構體的快速標記。