Home > Software design >  Storing Char and Int in an array and writing it to a Text File
Storing Char and Int in an array and writing it to a Text File

Time:03-30

I am trying to store char and int to the array 'employeeInfo' and write it to the text box 'KMARTEXT.txt'.

i am not sure on how to store in an array then to a text file.

I had the error in the addEmployee function.

 #include <stdio.h>
 #include<conio.h> // TEXT COLOR
 #include<stdlib.h>
 #include<string.h>
 #define g gotoxy
 #define p printf
 #define s scanf
 #define cp cprintf
 struct employeeInfo{ 
char employeeName[20];
int employeeBirthdate;
char employeeAddress[30];
int employeePhone;
};

FILE *pf, *tf, *temp;
int x, y;

int main(){
clrscr();
splash();
pickFunction();
getch();
}

splash(){
clrscr();
for(x=1;x<=80;x  ){
        g(x,1); p("%c",178); delay(20);
    }
    for(y=2;y<=24;y  ){
        g(80,y); p("%c",178); delay(20);
    }
    for(x=79;x>=1;x--){
        g(x,24); p("%c",178); delay(20);
    }
    for(y=23;y>=2;y--){
        g(1,y); p("%c",178); delay(20);
    }
textcolor(WHITE);
g(27,7); cp(" KMAR Employee Login System ");
}

pickFunction(){
char chosenOperation;
textcolor(WHITE);
g(31,11); cp("A.) Login");
g(31,12); cp("B.) Add Employee");
g(31,13); cp("C.) Delete Employee");
g(31,14); cp("D.) Search Employee");
g(31,15); cp("E.) Exit");
g(32,17); cp("Choose Operation: "); s("%s",&chosenOperation);

    switch(chosenOperation){
    case 'a': case 'A': loginEmployee(); break; /* Add/write to file function */
    case 'b': case 'B': addEmployee(); break; /* Search by num/string function */
    case 'c': case 'C': deleteEmployee(); break;
    case 'd': case 'D': searchEmployee(); break;
    case 'e': case 'E': exit(0); break;
    default: 
        textcolor(YELLOW);
        g(22,20); cp("Invalid Option! Press ENTER to go back..");
        getch();
        clrscr();
        main();
}
}

addEmployee(){
struct employeeInfo pck;
clrscr();
pf = fopen("KMARTEXT.txt", "w");

if(pf == NULL)
{
    /* File not created hence exit */
    printf("Unable to create file.\n");
    exit(EXIT_FAILURE);
}
g(31,12);
printf("Enter Employee Name: \n");
fgets(employeeInfo, employeeName, stdin);
fputs(employeeInfo, pf);
fclose(pf);
 return 0;
}

the error was Undefined symbol employeeInfo in function addEmployee

Undefined symbol employeeName in function addEmployee

CodePudding user response:

Your employeeinfo is a structure tag, not an actual instance of the struct. In other words, you described what such a structure contains, but you didn't create one.

You might want to review how to define structures: struct

CodePudding user response:

In addEmployee, you want:

fgets(pck.employeeName, sizeof pck.employeeName, stdin);
fputs(pck.employeeName, pf);
  • Related