简体   繁体   English

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

[英]how to read txt file and store in structure array in c

how do you read text and store in structure array??你如何阅读文本并存储在结构数组中?

I used while but is there any way using for??我使用了while,但是有什么方法可以使用吗??

  1. you need to use for你需要用于
  2. read the text until it is not EOF阅读文本,直到它不是 EOF
  3. you don't have to assume the file size您不必假设文件大小
  4. use &cast[i].使用 &cast[i]。 . . form形式
  5. there is no need to modify elsewhere.其他地方不需要修改。 I need to modify only read the file contents into cast[] part我只需要修改将文件内容读入 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

This code is not good enough, but should give you some hints.这段代码不够好,但应该给你一些提示。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C 中读取 .txt 文件并将数字存储到二维数组中 - How to read a .txt file in C and store the numbers into a 2D array 如何从.txt文件中读取文本,然后将其存储在记录(数据结构)中? - How to read text from a .txt file and then store it in a record (data structure)? 从.txt文件中读取数字并将它们存储在C中的数组中 - Read numbers from a .txt file and store them in array in C 读取txt文件中的数字列表并存储到数组C - Read list of numbers in txt file and store to array C 读取txt文件中的数字列表并以C格式存储到数组中 - Read list of numbers in txt file and store to array in C C语言帮助:如何将.txt文件中的字符串存储到字符数组中? 从命令行参数btw读取此.txt文件。 - C language help:How to store strings from a .txt file into a character array? This .txt file is read from a command line argument btw. 如何从 .t​​xt 文件中读取整数并将它们存储在 C 中的整数类型数组中 - How to read integers from .txt file and store them in a integer type array in C 如何从非结构化的.txt文件中读取单词并将每个单词存储在C中的char数组中? - How to read words from unstructured .txt file and store each word in a char array in C? 如何读取 TXT 文件并仅将 int 存储在 C 中 - How to read a TXT file and store only int in C 如何读取文本文件并存储在 C 中的数组中 - How to read a text file and store in an array in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM