0

我制作了文本文件 studenti.txt

  1. Gatis Lietnieks 15.06.1993 v
  2. Vizma Kalesnica 20.08.1991 年代
  3. Katrina Sabone 06.12.1992 年代
  4. Haralds Burkovskis 01.02.1989 v
  5. Jezups Martinovs 02.05.1990 v
  6. Vizma Zigurde 16.09.1988 年代
  7. 斯塔西娅·巴罗德 12.12.1993 年代
  8. Jānis Bērziņš 13.03.1992 v
  9. Zigurds Ritms 16.05.1990 v
  10. Pauls Zirdzins 12.11.1989 v
  11. 赞恩·斯卡布尔 28.12.1990 年代
  12. 艾加布勒 11.08.1993 年代
  13. 安德烈斯·福姆金斯 11.06.1989 v
  14. Maikls Dzordans 08.01.1988 v

我想读取文件并将其打印在 c 程序输出中。

我的代码是:

 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 #include <windows.h>
 #include <ctype.h>
 #define N 16
 int main()
 {
 FILE *fails_st;

 struct date
 { int da_year;
   int da_month;
   int da_day;
 };

 struct studenti
 {
 int Nr;
 char name[25];
 char surname[25];
 struct date dzd;
 char dzimums[1]; 
 } students[N];

 int i, j;
 system("cls");

 fails_st = fopen("studenti.txt", "r");
 for(i=0; i<N; i++)
 {
  fscanf(fails_st, "%d", &students[i].Nr);
  fgets(students[i].name, sizeof students[i].name, fails_st);
  fgets(students[i].surname, sizeof students[i].surname, fails_st);
  fscanf(fails_st, "%d", &students[i].dzd.da_day);
  fscanf(fails_st, "%d", &students[i].dzd.da_month);
  fscanf(fails_st, "%d", &students[i].dzd.da_year);
  fgets(students[i].dzimums, sizeof students[i].dzimums, fails_st);
  }
 fclose(fails_st);


 system("cls");

 printf("Student list\n");
 for(i=0; i<N; i++)
 printf("%d%s%s%d%d%d%s\n", students[i].Nr, 
 students[i].name, students[i].surname, 
 students[i].dzd.da_day,students[i].dzd.da_month,students[i].dzd.da_year,students[i].dzimums);
 getch();
 return 0;
 }

但是程序输出是这样的,我想知道为什么

Student list
1. Gatis Lietnieks 15.06.1993 v
202011158932
0. Vizma Kalesnica 20.08.1991 s
342685996
2130567168. Katrina Sabone 06.12.1992 s
48739784137
0. Haralds Burkovskis 01.02.1989 v
587162880
0. Jezups Martinovs 02.05.1990 v
626862441
0. Vizma Zigurde 16.09.1988 s
787397928
0. Stasija Balode 12.12.1993 s
987397848739786
0. JŌnis Bńrzi“­ 13.03.1992 v
1041984004198400
4096. Zigurds Ritms 16.05.1990 v
1126864728741408
  ....
    .................................
    .................................
   .................................
4

1 回答 1

1
  1. 首先使用 逐行读取fgets()
  2. 如果第一个字符是数字,则用于strtol转换为数字。
  3. 用于strtok断行 - 将字符串拆分为由点和空格分隔的标记

编辑:请找到上述步骤中描述的实现,以将文件读入结构。想法是使用所需的分隔符等读取每一行fgets()并使用解析它,strtok

    struct date
    { int da_year;
      int da_month;
      int da_day;
    };

    struct studenti
    {
      int Nr;
      char name[25];
      char surname[25];
      struct date dzd;
      char dzimums;
    } students[N];


    int main()
    {
       FILE *fails_st;
       char line[100];
       char *ptk; char * end; int i;

       fails_st = fopen("studenti.txt", "r");
       for(i=0; i < N && fgets(line, sizeof(line), fails_st) != NULL; i++)
       {
         students[i].Nr = strtol(line, &end, 10);

         ptk = strtok(line, " ");
         ptk = strtok(NULL, " ");
         strcpy(students[i].name, ptk);

         ptk = strtok(NULL, " ");
         strcpy(students[i].surname, ptk);

         ptk = strtok(NULL, ".");
         end = (ptk + strlen(ptk));
         students[i].dzd.da_day = strtol(ptk, &end, 10);

         ptk = strtok(NULL, ".");
         end = (ptk + strlen(ptk));
         students[i].dzd.da_month = strtol(ptk, &end, 10);

         ptk = strtok(NULL, " ");
         end = (ptk + strlen(ptk));
         students[i].dzd.da_year = strtol(ptk, &end, 10);

         ptk = strtok(NULL, " ");
         students[i].dzimums = *ptk;
       }
       fclose(fails_st);

       printf("Student list\n");
       for(i=0; i<N; i++)
       printf("%d. %s %s %d.%d.%d %c\n", students[i].Nr, students[i].name, 
              students[i].surname, students[i].dzd.da_day, 
              students[i].dzd.da_month, students[i].dzd.da_year, 
              students[i].dzimums);
       return 0;
    }
于 2013-11-13T18:22:09.533 回答