简体   繁体   English

如何从文本文件读取数据并将其作为结构存储?

[英]how data can be read from a text file and stored as as a structure?

Let's call this file f1.txt and it has given attributes. 我们将此文件称为f1.txt,它具有给定的属性。

  • Student code 学生代码
  • name 名称
  • ID ID

and resident structure from another file let's call f2.txt will be read with the following attributes 并从另一个文件中调用驻留结构f2.txt,它将读取以下属性

  • ID ID
  • City

and residence will be asked from keyboard. 然后将通过键盘询问居留权。

I tried to but gett stucked at some point 我试图在某个时候被卡住

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student
{
    int student_code;
    char name[20];
    char ID[20];
};

int main()
{
    FILE *input_file;
    struct student input;

    input_file = fopen("f1.txt", "r");
    if(input_file == NULL)
    {
        fprintf(stderr, "\nError!\n");
        exit(1);
    }

    while(fread(&input, sizeof(struct student), 1, input_file))
        printf("student code = %d name = %s ID = %s", input.student_code, 
input.name, input.ID);
    fclose(input_file);

return 0;

}

I'm new at C programming 我是C编程的新手

for example f1.txt file will be in the following format 例如f1.txt文件将采用以下格式

f1.txt
123456 yourname 987654
564566 test 454545
  1. Use fscanf to read the lines because you know the format 使用fscanf读取行,因为您知道格式

  2. Store the values into temporary variables 将值存储到临时变量中

  3. Copy them into a proper data structure: if you don't know the number of students use a dynamic array or a list. 将它们复制到适当的数据结构中:如果您不知道学生人数,则使用动态数组或列表。

EDIT: Arrays allow random access, on the other hand lists only allow sequential access. 编辑:数组允许随机访问,另一方面,列表仅允许顺序访问。 Here's my attempt using lists: 这是我尝试使用列表的方法:

typedef struct student
{
    int student_code;
    char name[20];
    char ID[20];
    struct student* next;
}student_t;

typedef struct list_s
{
    int size;
    student_t* head;
    student_t* tail;
} list_t;

void list_push_back(list_t* l, int code, char n[20], char id[20])
{
    student_t* tmp = (student_t*)calloc(1,sizeof(*tmp));
    tmp->student_code = code;
    strncpy(tmp->name, n, 20);
    strncpy(tmp->ID, id, 20);

    if (l->head == NULL)
    {
        l->head = tmp;
        l->tail = tmp;
        l->size++;
        return;
    }

    l->tail->next = tmp;
    l->size++;
}

void list_print(list_t* l)
{
    student_t* it = l->head;

    while (it)
    {
        fprintf(stdout, "%d %s %s\n", it->student_code, it->name, it->ID);
        it = it->next;
    }
}

int main(int argc, char* argv[])
{
    FILE *input_file;
    struct student input;

    input_file = fopen("f1.txt", "r");
    if (input_file == NULL)
    {
        fprintf(stderr, "\nError!\n");
        exit(1);
    }

    list_t* list = (list_t*)calloc(1,sizeof(*list));

    char tmp_name[20];
    char tmp_id[20];
    int tmp_code = 0;

    while (fscanf(input_file, "%d %s %s", &tmp_code, tmp_name, tmp_id) != EOF)
    {
        list_push_back(list, tmp_code, tmp_name, tmp_id);
    }

    fclose(input_file);

    list_print(list);

    return 0;
}

To read a single record into the structure input : 要将单个记录读入结构input

fscanf( input_file, "%d %s %s", &input.student_code, input.name, input.ID )

to read a integer and two strings into the relevant members. 将一个整数和两个字符串读入相关成员。

fscanf() returns the number of formatted fields successfully assigned, so to display all records in the file in the manner your code attempts: fscanf()返回成功分配的格式化字段的数量,以便以您的代码尝试的方式显示文件中的所有记录:

while( fscanf( input_file, "%d %s %s", &input.student_code, 
                                       input.name, 
                                       input.ID ) == 3 )
{
    printf( "student code = %d, name = %s, ID = %s\n", input.student_code,
                                                       input.name, 
                                                       input.ID ) ;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM