如何用fscanf 把txt file 里的资料store进我的program里?
我要把StudentDetails.txt里的资料save进我的program,我用了很多方法做不到。。。不知道有人可以解答吗..? 我compile了,就是没output ....
这里是我的code...
#include<stdio.h>
#include<stdlib.h>
#define NO_OF_WEEKS 14
#define NO_OF_STUDS 5
void looping();
void List_Student_Details();
typedef struct
{
int year;
char campus;
char school;
char level;
char sn[6];
}regNUM;
regNUM regno;
typedef struct
{
int studno;
regNUM regno;
char surname[5];
char givenname[8];
char gender;
char fathername[7];
char addr1[12];
char addr2[12];
char addr3[6];
}Student;
Student details[NO_OF_STUDS];
int main()
{
looping();
}
void looping()
{
int ans=0;
printf("Please Choose an option on this CLASS ATTENDANCE SYSTEM.\n");
printf("[1]Display the List of Student Details.\n");
printf("[6]Display the Poor Attendance list.\n");
printf("[7]Exit.\n");
printf("Your Choice is ...... ");
scanf("%d", &ans);
if(ans==1)
{
List_Student_Details();
}
else
{
printf("Bye bye.\n");
}
}
void List_Student_Details()
{
int n=0;
FILE *stud_d;
stud_d=fopen("StudentDetails.txt","r");
printf("FUCKER");
while(fscanf(stud_d,"%d|%d%c%c%c%[^|]|%[^|]|%[^|]|%c|%[^|]|%[^|]|%[^|]|%[^\n]\n",
&details[n].studno,
&details[n].regno.year,
&details[n].regno.campus,
&details[n].regno.level,
details[n].regno.sn,
details[n].surname,
details[n].givenname,
&details[n].gender,
details[n].fathername,
details[n].addr1,
details[n].addr2,
details[n].addr3)!=EOF)
{
printf("%d%d%c%c%c%s", details[n].studno, details[n].regno.year, details[n].regno.campus, details[n].regno.school, details[n].regno.level, details[n].regno.sn);
n++;
}
fclose(stud_d);
}
这个是我的txt文件内容:
1|10WAD03824|Ang|San Nin|M|Louis|87 jln bola|taman raya|KL
2|10WAD07575|Gan|Kel Lee|F|Andy|lorong 65|Taman PJ|perak
3|10WAD06321|Chan|Mel vin|M|Ben|jln 218|taman KP|KL
4|10WAD07819|Lau|Mei Lee|F|Christ|88 jln ipoh|Taman petro|kedah
5|10WAD05208|Khan|Ken See|M|jesus|77 jln ultra|taman bb|KL