简体   繁体   English

使用 fscanf 从 .txt 文件中读取内容并存储在结构中

[英]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.所以我在制作一个从 .txt 文件读取内容并将其存储在结构中的程序时遇到了麻烦。 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.我已经尝试了很多方法来做到这一点(比如写fscanf(input, "%d/%d/%d",...)和事情一样奇怪)并在很多地方寻求帮助,但它只是没有不工作,我找不到问题所在。 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我也更喜欢fscanf而不是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

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

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