标题:如何用fscanf 把txt file 里的资料store进我的program里?
只看楼主
aznelite89
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-11-25
 问题点数:0 回复次数:1 
如何用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
搜索更多相关主题的帖子: txt program fscanf store file 
2010-11-25 17:41
aznelite89
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-11-25
得分:0 
i realize that the value stored in details[n].school is null or spacebar , howto deal with problem to avoid fscanf the %c(\0) from text file into my program?(用translate. 汉化,不大会说华语)

here is my 2nd test program... pls leave some comment about it..
程序代码:
#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("\nREPORT before fscanf\n");
    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\n%d\n%c\n%c\n%c\n", details[n].studno, details[n].regno.year,details[n].regno.campus,details[n].regno.school,details[n].regno.level);
        n++;
    }
    fclose(stud_d);
    looping();
}



[ 本帖最后由 aznelite89 于 2010-11-25 18:10 编辑 ]
2010-11-25 18:09



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-326886-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.412563 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved