so i'm reading in file linked list. reads in okay except final field "email" reads null. have ideas?
include <stdlib.h> #include <stdio.h> #include <string.h> #define id_length 25 #define name_length 25 #define address_length 25 #define department_length 25 #define joindate_length 25 #define email_length 30 //create employee structure typedef struct employee{ char* id; char* name; char* address; char* department; char* joindate; double salary; char* email; struct employee *next; }employee; //declare function prototypes employee* new_employee(char*, char*, char*, char*, char*, double, char*); employee* insert_by_employee(employee*, employee*); void print_list(employee*); int main() { file *in; char* id = (char*)malloc(sizeof(char*) * id_length); char* name = (char*)malloc(sizeof(char*) * name_length); char* department = (char*)malloc(sizeof(char*) * department_length); char* address = (char*)malloc(sizeof(char*) * address_length); char* joindate = (char*)malloc(sizeof(char*) * joindate_length); double salary = 0; char* email = (char*)malloc(sizeof(char*) * email_length); if ((in = fopen("employees.txt", "r")) == null) //did file open? { printf("the input file failed open.\n"); printf("program cannot continue. exiting. . .\n"); return 1; //exit program } //2. ------linked list operations------ employee* head = null; //create empty linked list employee* current = null; while (!feof(in)) //check file end { //read first data value kickstart. if (fscanf(in, "%s %s %s %s %s %d %s", id, name, department, address, joindate, &salary, email) == eof) { break; } employee* hold = new_employee(id, name, department, address, joindate, salary, email); head = insert_by_employee(head, hold); } //3. ------print new list------ print_list(head); system("pause"); return 1; //exit success } employee* new_employee(char* id, char* name, char* department, char* address, char* joindate, double salary, char* email) { //create new employee , malloc space employee* new = (employee*)malloc(sizeof(struct employee)); new->id = (char*)malloc(sizeof(char) * id_length); new->name = (char*)malloc(sizeof(char) * name_length); new->department = (char*)malloc(sizeof(char) * department_length); new->address = (char*)malloc(sizeof(char) * address_length); new->joindate = (char*)malloc(sizeof(char) * joindate_length); new->email = (char*)malloc(sizeof(char) * email_length); //set data strcpy(new->id, id); strcpy(new->name, name); strcpy(new->department, department); strcpy(new->address, address); strcpy(new->joindate, joindate); new->salary = salary; strcpy(new->email, email); //retun pointer node return new; } //inserts new node alphabetically sorted linked list. employee* insert_by_employee(employee* head, employee* new) { employee* current = null; current = head; if (current == null || strcmp(current->name, new->name) > 0) { new->next = current; return new; } else { while (current->next != null && strcmp(current->next->name, new->name) < 0) { current = current->next; } } new->next = current->next; current->next = new; return head; } void print_list(employee* head) { employee* current; current = head; char i[] = "employee id"; char n[] = "name"; char a[] = "address"; char d[] = "department"; char jd[] = "join date"; char s[] = "salary"; char em[] = "email"; //header printf("\n\n|%15s | %15s | %15s | %15s | %15s| %15s | %15s|\n", i, n, a, d, jd, s, em); printf("--------------------------------------------------------------------------------------------------------------------------------------------\n"); while (current != null) { printf("|%15s | %15s | %15s |%15s | %15s | %15d| %15s\n", current->id, current->name, current->address, current->department, current->joindate, current->salary, current->email); current = current->next; } printf("-------------------------------------------------------------------------------------------------------------------------------------------\n"); return; }
here sample file inputting data linked list
emp1 martin clare computer 06/06/1996 60000 hannanmartin@gmail.com emp2 john galway accounting 05/05/1995 50000 johnemp@gmail.com emp3 kevin kilkenny computer 04/04/1994 34000 kevinemp@gmail.com emp4 kim sligo sales 03/03/1993 65000 kimemp@gmail.com emp5 keith ballina accounting 02/02/1992 53200 keithemp@gmail.com emp6 tim dublin sales 01/01/1991 23000 timemp@gmail.com emp7 andy chicago computer 07/07/1990 10000 andyemp@gmail.com
salary double not int. have been using %d
in both scanf , printf calls. either use %lf
doubles, or change salary
int.