如何读取txt文件并存储在c中的结构数组中

Posted

技术标签:

【中文标题】如何读取txt文件并存储在c中的结构数组中【英文标题】:how to read txt file and store in structure array in c 【发布时间】:2022-01-05 00:38:31 【问题描述】:

如何读取文本并存储在结构数组中??

我用了while,但是有什么办法用for吗??

    你需要用于 阅读文本直到它不是 EOF 您不必假设文件大小 使用 &cast[i] 。 .表格 其他地方不需要修改。 我只需要修改将文件内容读入 cast[] 部分
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct names 
    char  first[20];
    char  last[20];
;

struct date 
    char  month[12];
    int  day,  year;
;

struct person 
    struct  names  name;
    struct  date  birthday;
;

/* Converts "January", "February", ..., "December" 
   into corresponding numbers 1, 2, ..., 12 */
int convert (char *mon)  
    static const char *month[] =  "January", "February","March","April","May","June","July","August",
        "September","October", "November","December", NULL ;

    for (int i = 0; month[i] != NULL; i++) 
        if (strcmp(month[i], mon) == 0) 
            return i + 1;
        

    
    return -1;


/* argv[1] contains the filename: cast.txt */
main(int argc, char  *argv[]) 
    struct  person  cast[20];
    int  ncast = 0;
    FILE  *f;
    int  i;

    if (argc < 2) 
        fprintf(stderr,  "usage:  %s  filename\n?? argv[0]);
        exit(1);
    

    if ((f = fopen(argv[1], "r")) == NULL) 
        fprintf(stderr,  "%s: can't open %s\n", argv[0], argv[1]);
        exit(1);
    

 /* Reads the file contents into cast[] */ << this is where I need help 
int i = 0;
    while ((fscanf(f, "%s", "%s", "%d", "%d", "%d",
        %cast[i].name.last,
        %cast[i].name.first,
        %cast[i].birthday.month,
        %cast[i].birthday.day,
        %cast[i].birthday.year)) != EOf) 

        i++;
    

fclose(f);

    printf("Cast of Captain America: Civil War\n");
    printf("==================================\n\n");
    printf("Name   (Birthday)\n\n");
    for (i = 0; i < ncast; i++)
        printf("%s, %s  (%02d/%02d/%02d)\n",
            cast[i].name.last, 
            cast[i].name.first,
            convert(cast[i].birthday.month),
            cast[i].birthday.day,
            cast[i].birthday.year % 100);
     



here is the text file 

Chris Evans  June 13, 1981
Robert Downey  April 4, 1965
Scarlett Johansson  November 22, 1984
Sebastian Stan  August 13, 1982 
Anthony Mackie  September 23, 1978
Don Cheadle  November 29, 1964 
Jeremy Renner  January 7, 1971  
Chadwick Boseman  November 29, 1976 
Paul Bettany  May 27, 1971
Elizabeth Olsen  February 16, 1989
Paul Rudd  April 6, 1969
Emily VanCamp  May 12, 1986
Tom Holland  June 1, 1996 
Daniel Bruhl  June 16, 1978
Frank Grillo  June 8, 1965

【问题讨论】:

【参考方案1】:

这段代码不够好,但应该给你一些提示。

    for (int i = 0; i < 20 && !feof(f); ++i) 
        if (fscanf(f, "%s %s %s %d, %d", cast[i].name.last,
                   cast[i].name.first, cast[i].birthday.month,
                   &cast[i].birthday.day, &cast[i].birthday.year) != 5) 
            break;
        
    

【讨论】:

以上是关于如何读取txt文件并存储在c中的结构数组中的主要内容,如果未能解决你的问题,请参考以下文章

c语言 如何读取中文字符串

C 编程:读取文件并存储在结构数组中

如何将 .txt 文件中的特定整数存储在结构数组中?

C程序:从文件中读取矩阵数据,并显示出来,利用链式存储结构。

怎样用C语言读取txt文件中的二进制数据并转为一维数组

如何用C语言读取txt文件中的数据到结构体数组中