gpt4 book ai didi

c++ - 结构和文件

转载 作者:行者123 更新时间:2023-11-28 07:17:45 24 4
gpt4 key购买 nike

我制作了文本文件studenti.txt

  1. Gatis Lietnieks 15.06.1993 v
  2. Vizma Kalesnica 20.08.1991 s
  3. 卡特里娜飓风 06.12.1992 s
  4. Haralds Burkovskis 01.02.1989 v
  5. Jezups Martinovs 02.05.1990 v
  6. Vizma Zigurde 16.09.1988 s
  7. Stasija Balode 1993 年 12 月 12 日
  8. Jānis Bērziņš 13.03.1992 v
  9. Zigurds Ritms 16.05.1990 v
  10. Pauls Zirdzins 12.11.1989 v
  11. Zane Skarbule 28.12.1990 s
  12. Aiga Bulle 11.08.1993 s
  13. Andrejs Fomkins 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
....
.................................
.................................
.................................

最佳答案

  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;
}

关于c++ - 结构和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19959837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com