将未格式化的文件复制到结构中

时间:2014-04-05 13:08:21

标签: c string struct io

#define MAX 20
typedef char string20[21];

struct flight_list{
    char fcode[21];
    string20 srccity;
    string20 descity;
    int deptime[2];/** deptime[0] hours, deptime[1] minutes **/
    int duration;
    int eta[2];/**eta[0] hours , eta[1] minutes**/
    int nctr;

};
void load_flight(struct flight_list flight[]){
    int num,i;
    char* ptr;
    FILE* fp;
    fp = fopen("flightFILES.txt","r");
    fscanf(fp,"%d",&num);
    while(!feof(fp)){
        for(i=0;i<num;i++){
            fgets(flight[i].fcode,100,fp);
            fgets(flight[i].srccity,100,fp);
            fgets(flight[i].descity,100,fp);
            fscanf(fp,"%d %d",flight[i].deptime[0],flight[i].deptime[1]);
            fscanf(fp,"%d",flight[i].duration);
            fscanf(fp,"%d %d",flight[i].eta[0],flight[i].eta[1]);

            ptr=strchr(flight[i].fcode,'=');
            strcpy(flight[i].fcode,ptr+1);

            ptr=strchr(flight[i].srccity,'=');
            strcpy(flight[i].srccity,ptr+1);

            ptr=strchr(flight[i].descity,'=');
            strcpy(flight[i].descity,ptr+1);
        }
    }

    fclose(fp);



}

每次我运行这个功能它崩溃我只是按照他建议我做的事请帮助我我想把这个文件里面的内容复制到我的结构数组中 文件格式为:

4
FCODE=CHIX
SRCCITY=MAN
DESTCITY=KAN
DEPARTURE=12 10
DURATION=30
FCODE=PAUL
SRCCITY=KAN
DESTCITY=CHG
DEPARTURE=13 10
DURATIOn=60

File input/output in C unformatted

0 个答案:

没有答案