如何使用文件填充结构(下面附有代码)?

我正在尝试使用底部给出的文件填充结构,请在代码中建议一些编辑,以便我可以像在main()函数中尝试过的那样打印结构,这是我第一次在stackoverflow上进行,所以请原谅我在发布此问题期间犯的一些错误。

文件内容一个接一个,另一个是结构声明的顺序(可变位置相同)

请指导我正确填充该结构并在main()函数中进行打印。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
//this code works on ZERO INDEXING
struct Empdetials
{
char name[25];
char id[8];
char password[4];
int casual;
int medical;
int earned;
}emp[5];//declaring array of structures
//I am trying to convert the file to a structure and then use the structure to access the data and 
I'm also using files to store the information 
extern void table()
{
FILE *fp;//definig a file pointer
char ch;
char  line[4096];
char *token;//token pointer that points to a 
int tokenposition=0;//token here means the particular text seperqated by comma
int lineposition=0;//line here is the entire line which has entire details of the an emmployee 
fp=fopen("emplyoyeeinfo.txt","r");//opening the file and reading it
while(fgets(line,4096,fp) !=NULL) //accessing the lines one by one and using a \n to recognize the end of a line
{
    tokenposition=0;//when we tokenize the line we need the count to keep a track of where we are int he string
   // printf("%s \n",line);//used to check which line is not being read by the compiler
    token = strtok(line,",");//this seperates a line into token(entity) based on a delimiter,here the delimiter is a comma
    //string tokenization` 
    while(token != NULL)
    {
        switch(tokenposition)//acessing the required element through the tokenposition counter
        {
            case 0:
                strcpy(emp[lineposition].name,token);
                break;
            case 1:
                strcpy(emp[lineposition].id,token);
                break;
            case 2:
                strcpy(emp[lineposition].password,token);
                break;
            case 3:
                emp[lineposition].casual=atoi(token);
                break;
            case 4:
               emp[lineposition].medical=atoi(token);
                break;
            case 5:
                emp[lineposition].earned=atoi(token);
                break;
        }
        token =strtok(NULL,",");
    tokenposition++;//to access the next token in the same line
    }
    lineposition++;//after all the tokens are put in a structure,wwe move to the next line and redo the entire process
}    
}
}
void main()
{
int i;
for (i = 0; i < 5; i++)
{
        printf("\033[34m\n\n\tNAME : %s\033[0m\n", emp[i].name);
        printf("\033[34m\n\n\tID : %s\033[0m\n", emp[i].id);
        printf("\033[34m\n\n\tPASSWORD : %d\033[0m\n", emp[i].password);
        printf("\033[34m\n\n\tCASUAL : %f\033[0m\n", emp[i].casual);
        printf("\033[34m\n\n\tMEDICAL : %c\033[0m\n", emp[i].medical);
        printf("\033[34m\n\n\tEARNED : %s\033[0m\n", emp[i].earned);
}}

文件是(名称,用户名,密码,请假,请假,请假,请假

Ramesh,QW120345,PO56,10,15,7
Rajesh,QW120905,IO56,10,15,7
Kajal,JI456987,IWQ9,10,15,7
Harleen,HJ782013,ZM12,10,15,7
Jim,BN784569,KL45,10,15,7

output of the code when trying to print the structure is in the screenshot

评论
  • 初吻给了烟
    初吻给了烟 回复

    两件事情:

    1. You need to actually call the table() function from your main() to invoke it before trying to print the contents of the structure. Otherwise, the structure (and any other variables in C) is initially populated with garbage values.
    2. The printf() statements in your main() are not using the correct format specifiers to match the variables types defined in the Empdeials structure. Make sure you use the correct specifiers (see this page for further info on the specifiers: http://www.cplusplus.com/reference/cstdio/printf/)