简体   繁体   中英

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.

  • Student code
  • name
  • ID

and resident structure from another file let's call f2.txt will be read with the following attributes

  • 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

for example f1.txt file will be in the following format

f1.txt
123456 yourname 987654
564566 test 454545
  1. Use fscanf to read the lines because you know the format

  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 :

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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