读取文本文件格式时遇到问题

发布于 2025-01-09 12:39:44 字数 2447 浏览 0 评论 0原文

我无法正确读取文本文件。我相信我正确使用了 fscanf,但没有正确使用文本文件本身的 printf 格式。

//student struc
struct student
{
    char header[50], name[20];
    int student_no;
    float subjectA,subjectB;
};


//Read data in file and store in struc
int readData(struct student stud[])
{
    char fname[70];
    printf("Please enter input file name:");
    scanf("%123s",fname); // scans the file name inputted
    strcat(fname, ".txt");
    printf("%s \n", fname);
   FILE *fp;//  pointer
   int i;
   fp=fopen(fname,"r" );//open file in read
   if(fp==NULL) //unable to open the file
   {
      exit(0);
   }
   i=0;
    
    
   //reads data and stores in student struc
   while(fscanf(fp,"%s%s%d%f%f",stud[i].header,stud[i].name,&stud[i].student_no,&stud[i].subjectA,&stud[i].subjectB)!=EOF)
   {
       i++;
   }
    
   fclose(fp);
   return i;
}


// print data function
void printData(struct student stud[],int size)
{
    int i;
    for(i=0;i<size;i++) // constantly populates the struc
    printf("\n \%-14s\%s\t\t%d\t\t%0.1f\t\t%0.1f",stud[i].header,stud[i].name,stud[i].student_no,stud[i].subjectA,stud[i].subjectB);

int main()
{
   struct student stud[100];
   int n;
    char opt = '\0';
    float sdA = 0.0,sdB = 0.0,avgA = 0.0,avgB = 0.0;
   n=readData(stud); //readData function
   printData(stud,n);//printData
    printf("\n");

这是输入文件

STUDENT NAME    STUDENT NO  SUBJECT A   SUBJECT B
JOAN            1           70.5        85
TANIA           2           49          75
TOM             3           53          54
JEFF            4           80          49.5
SUSAN           5           20          20
KATHY           6           99          55
RAYMOND         7           22.5        75

这是我收到的输出。它应该表示与输入文本文件相同的格式。即使我去掉空格,程序仍然读取标题的下一行(文件顶部),然后创建一个列。

STUDENT       NAME      1600735199      0.0     259660666015580160.0
 STUDENT       NO       32767       -230262611306872832.0       0.0
 SUBJECT       A        1644026974      0.0     0.0
 SUBJECT       B        32767       -226721427951190016.0       0.0
 JOAN          1        70      0.5     85.0
 TANIA         2        49      75.0        0.0
 TOM           3        53      54.0        4558848.0
 JEFF          4        80      49.5        0.0
 SUSAN         5        20      20.0        168040595456.0
 KATHY         6        99      55.0        0.0
 RAYMOND       7        22      0.5     75.0

I'm having trouble reading the text file properly. I believe I'm using the fscanf properly but not the formatting of the printf of the text file itself.

//student struc
struct student
{
    char header[50], name[20];
    int student_no;
    float subjectA,subjectB;
};


//Read data in file and store in struc
int readData(struct student stud[])
{
    char fname[70];
    printf("Please enter input file name:");
    scanf("%123s",fname); // scans the file name inputted
    strcat(fname, ".txt");
    printf("%s \n", fname);
   FILE *fp;//  pointer
   int i;
   fp=fopen(fname,"r" );//open file in read
   if(fp==NULL) //unable to open the file
   {
      exit(0);
   }
   i=0;
    
    
   //reads data and stores in student struc
   while(fscanf(fp,"%s%s%d%f%f",stud[i].header,stud[i].name,&stud[i].student_no,&stud[i].subjectA,&stud[i].subjectB)!=EOF)
   {
       i++;
   }
    
   fclose(fp);
   return i;
}


// print data function
void printData(struct student stud[],int size)
{
    int i;
    for(i=0;i<size;i++) // constantly populates the struc
    printf("\n \%-14s\%s\t\t%d\t\t%0.1f\t\t%0.1f",stud[i].header,stud[i].name,stud[i].student_no,stud[i].subjectA,stud[i].subjectB);

int main()
{
   struct student stud[100];
   int n;
    char opt = '\0';
    float sdA = 0.0,sdB = 0.0,avgA = 0.0,avgB = 0.0;
   n=readData(stud); //readData function
   printData(stud,n);//printData
    printf("\n");

This is the input file

STUDENT NAME    STUDENT NO  SUBJECT A   SUBJECT B
JOAN            1           70.5        85
TANIA           2           49          75
TOM             3           53          54
JEFF            4           80          49.5
SUSAN           5           20          20
KATHY           6           99          55
RAYMOND         7           22.5        75

This is the output I'm receiving. It should represent the same format as the input text file. Even when I take out the spaces, the program still reads the next line of the heade (top of the file) then creates a column.

STUDENT       NAME      1600735199      0.0     259660666015580160.0
 STUDENT       NO       32767       -230262611306872832.0       0.0
 SUBJECT       A        1644026974      0.0     0.0
 SUBJECT       B        32767       -226721427951190016.0       0.0
 JOAN          1        70      0.5     85.0
 TANIA         2        49      75.0        0.0
 TOM           3        53      54.0        4558848.0
 JEFF          4        80      49.5        0.0
 SUSAN         5        20      20.0        168040595456.0
 KATHY         6        99      55.0        0.0
 RAYMOND       7        22      0.5     75.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文