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

从.txt文件读取内容并使用fscanf存储在结构中,c,C,所以我很难制作一个程序,从.txt文件中读取内容并将其存储在结构中。该文件在内部大致如下所示: 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/

所以我很难制作一个程序,从.txt文件中读取内容并将其存储在结构中。该文件在内部大致如下所示:

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)
...
我尝试过很多方法(比如写
fscanf(输入,“%d/%d/%d”…)
和一些同样奇怪的东西)并在很多地方寻求帮助,但都不起作用,我找不到问题所在。任何帮助都将不胜感激

下面是我要说的代码部分:

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);
    }
typedef结构dt
{
国际日;
整月;
国际年;
}日期;
类型定义结构如果
{
字符名[50];
浮动工资;
开始日期;
查部[50];
}信息;
int main(int argc,字符**argv){
inti,n;
文件*输入;
输入=fopen(argv[1],“r”);
如果(输入==NULL)
{
printf(“打开文件失败”);
返回1;
}
fscanf(输入,“%d”&n);
信息数据库[n];
fseek(输入,sizeof(n),SEEK_集);
对于(i=0;i
我试图修复您的代码,请阅读下面的修复代码以及我在导致问题的行上的评论。此外,我更喜欢
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);
    }
}

我正在尝试修复您的代码,请阅读下面的修复代码以及我在导致问题的行上的注释。此外,我更喜欢
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);
    }
}