第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么無法從文件中讀取正確的信息?Orz求大神(任務(wù)5)

為什么無法從文件中讀取正確的信息?Orz求大神(任務(wù)5)

C
絕對不乖 2016-12-27 14:56:49
/*2.定義一個結(jié)構(gòu)體變量struct student, 4個成員,學(xué)號,姓名,科目和成績 (兩個結(jié)合成一個成員),還有一個本結(jié)構(gòu)體的指針任務(wù)一: 在main函數(shù)中定義一個struct student結(jié)構(gòu)體數(shù)組,其長度為5任務(wù)2: 編寫一個函數(shù)用于從鍵盤上輸入5個學(xué)生信息任務(wù)3: 編寫函數(shù)將第二題的結(jié)構(gòu)體數(shù)組中的5個學(xué)生的信息用動態(tài)分配內(nèi)存的方式組成一個單鏈表再編寫一個函數(shù)輸出到桌面。任務(wù)4: 將第3題中的單鏈表的5個節(jié)點依次存入文件file任務(wù)五: 從第四題中文件file.txt讀取第1,3,5個節(jié)點界面輸出并存入文件file2.txt*/#include <stdio.h>#include <stdlib.h>#define N 5//可以改的數(shù)#define LEN sizeof(struct Student) //長度為結(jié)構(gòu)體的長度struct Student{ char num; char name[10]; char sub[10]; int score; struct Student *next;};void inputandoutput(struct Student stu[])//輸入數(shù)組和打印出來{ int i; for(i=0;i<N;i++) { scanf("%c %s %s %d",&stu[i].num,stu[i].name,stu[i].sub,&stu[i].score); stu[i].next=NULL; getchar(); } for(i=0;i<N;i++) { printf("No.%c %s %s %d\n",stu[i].num,stu[i].name,stu[i].sub,stu[i].score); } }void line(struct Student stu[])//動態(tài)鏈表{ struct Student *p1,*p2; int i; FILE *fp; p1=p2=(struct Student *)malloc(LEN);//給指針分配空間 for(i=1;i<=N;i++) { if(i==1) p1=stu; else p2->next=p1; p2=p1; p1=(struct Student *)malloc(LEN); p1=&stu[i]; } printf("現(xiàn)在輸出鏈表:\n");//輸出動態(tài)鏈表 p1=stu; for(i=0;i<N;i++) { printf("No.%c %s %s %d\n",p1->num,p1->name,p1->sub,p1->score); p1=p1->next; } p1=stu;//和輸出列表相似,把列表通過fprintf打入到文件中 if((fp=fopen("flim.txt","wt"))==NULL) { printf("eorre\n"); exit(0); } while(p1) { fprintf(fp,"%c %s %s %d\n",p1->num,p1->name,p1->sub,p1->score);//指針,格式,變量//這里用了fprintf也可以用fwrite輸入 p1=p1->next;//下一個 } fprintf(fp,"\n"); fclose(fp);}void read(struct Student stu[]){ FILE *fp,*fp2; int i,n; struct Student stu2[N]={'\0'}; if((fp=fopen("flim.txt","rb"))==NULL) { printf("error\n"); exit(0); } for(i=0;i<N;i+=2) { fseek(fp,i*sizeof(stu[i]),0); fread(&stu2[i],sizeof(struct Student),1,fp); printf("No.%c %s %s %d\n",stu2[i].num,stu2[i].name,stu2[i].sub,stu2[i].score); } if((fp2=fopen("film2.txt","wt"))==NULL) { printf("error\n"); exit(0); } for(i=0;i<N;i+=2) { fwrite(&stu2[i],sizeof(struct Student),1,fp2); } fclose(fp); fclose(fp2);}int main(){ int i; struct Student stu[N]; inputandoutput(stu); line(stu); for(i=0;i<N;i++) printf("一個結(jié)構(gòu)體的字節(jié)數(shù)為:%d\n",sizeof(stu[i])); read(stu); return 0;}
查看完整描述

1 回答

?
angie

TA貢獻(xiàn)56條經(jīng)驗 獲得超8個贊

你用 fprintf 寫入的,咋不用 fscanf 讀入呢? ?可以這么理解吧,fprintf 和 fscanf 操作的是字符串,而 fwrite 和 fread 操作的是二進(jìn)制

查看完整回答
反對 回復(fù) 2017-01-31
  • 1 回答
  • 1 關(guān)注
  • 1153 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號