简体   繁体   中英

Reading content from a .txt file and storing in a struct using fscanf

So I'm having trouble making a program that reads content from a .txt file and store it in a struct. The file looks more or less like this in the inside:

number that tells how many employees have their information stored in the file

name (string)

salary (float like 2000.00)

the date of when they started at the job (in the format dd/mm/yyyy like 22/10/2020)

department (string)
...

I've tried many ways of doing this (like writing fscanf(input, "%d/%d/%d",...) and things just as weird) and looked for help in lots of places, but it just doesn't work, I can't find what's wrong. Any help would be much appreciated!

Here's the part of the code I'm talking about:

typedef struct dt
{
    int day;
    int month;
    int year;
} date;

typedef struct if
{
    char name[50];
    float salary;
    date start;
    char department[50];
} info;

int main(int argc, char **argv){

    int i, n;
    FILE *input;

    input = fopen(argv[1], "r");

    if(input == NULL)
    {
        printf("Failed to open the file");
        return 1;
    }

    fscanf(input, "%d", &n);

    info database[n];

    fseek(input, sizeof(n), SEEK_SET);

    for(i = 0; i < n; i++)
    {
        fgets(database[i].name, 100, input);
        fscanf(input, "%f", &database[i].salary);
        fscanf(input, "%2d", &database[i].start.day);
        fscanf(input, "%2d", &database[i].start.month);
        fscanf(input, "%4d", &database[i].start.year);
        fgets(database[i].department, 25, input);
    }

I trying to fix your code, read the fixed code below and my comment on the line that's cause the problem. Also i prefer fscanf instead of fgets

#include <stdio.h>

typedef struct dt{
    int day;
    int month;
    int year;
} date;

typedef struct inf{
    char name[50];
    float salary;
    date start;
    char department[50];
} info;

int main(int argc, char **argv){

    int i, n;
    FILE *input;

    input = fopen(argv[1], "r");

    if(input == NULL)
    {
        printf("Failed to open the file");
        return 1;
    }

    fscanf(input, "%d", &n);

    info database[n];

    // fix sizeof(n) to sizeof(n)-1
    fseek(input, sizeof(n)-1, SEEK_SET);

    for(i = 0; i < n; i++)
    {
        // using fscanf to get name
        fscanf(input, "%100s",database[i].name);
        fscanf(input, "%f", &database[i].salary);
        // using fscanf to get day/month/year
        fscanf(input, "%2d/%2d/%4d", &database[i].start.day, &database[i].start.month, &database[i].start.year);
        // using fscanf to get the department string
        fscanf(input, "%25s", database[i].department);
    }
}

here is my sample text file

3
john
200.00
22/10/2020
lolba
jea
250.00
22/10/2120
lilba
jug
270.00
22/10/2220
logba

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