I am having problems with segmentation fault. When I run this particular code on a windows machine with devc++ or codeblocks it runs how I intended. The problem occurs when I try and run on a linux machine. I was told that it was most likely due to pointers trying to access locations they are not allowed but i cannot figure out why.
When I run the program without the "empinfo.txt" file it will start and allow me to do any of the menu options except for "1-Add", so the problem seems to have something to do with the way I am using arrays with my structure.
I included all of the code but the only problems functions(i think) are initialize and add. Any help on what is the correct way to use an array of chars in this case would be highly appreciated.
Here is sample input that would be in empinfo.txt file
12 JackSprat 2 1 65000
13 HumptyDumpty 5 3 30000
17 BoPeep 2 3 30000
20 BoyBlue 3 2 58000
0
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *empinfo=NULL;
struct employeeData {
int EMP_ID;
char name[20];
int dept;
int rank;
double salary;
struct employeeData *next;
};
struct employeeData *head = NULL;
void initializeList();
void add(int ID, char name[0], int dept, int rank, double newSalary);
void deleteEmp(int ID);
void modify(int ID, double NewSalary);
void query(int rank);
void print();
int main(){
int choice=1, ID=0, rank=0, dept=0;
double newSalary=0;
char name[20];
initializeList();
while (choice != 0) {
printf("1-Add 2-Delete 3-Modify 4-Query 5-Print 0-Exit\n");
fflush(stdin);
scanf("%d", &choice);
if (choice == 1) {
printf("Enter new employee info. Format: \"ID name dept rank salary\"\n");
if((scanf("%d %s %d %d %lf", &ID, name, &dept, &rank, &newSalary))==5)
add(ID, name, dept, rank, newSalary);
else{
printf("invalid format entered\n\n");
continue;
}
}
else if (choice == 2){
printf("Enter employee ID to delete: ");
if(scanf("%d", &ID)==1){
deleteEmp(ID);}
else{
printf("Employee ID must be an integer\n");
continue;
}
}
else if (choice == 3){
printf("Enter employee ID to modify: ");
if(scanf("%d", &ID)==1){
printf("Enter new salary amount: ");
if(scanf("%lf", &newSalary)==1)
modify(ID,newSalary);
else
printf("Salary must be a number\n");
}
else{
printf("Employee ID must be an integer\n");
continue;
}
}
else if (choice == 4){
printf("Enter the rank you wish to query: ");
scanf("%d", &rank);
query(rank);
}
else if (choice == 5){
print();
}
}
printf("Goodbye...\n");
head=NULL;
free(head);
return 0;
}
void initializeList(){
empinfo=fopen("empinfo.txt", "r");
head = (struct employeeData *)malloc(sizeof(struct employeeData));
head->next = NULL;
if (empinfo==NULL){
printf("empinfo.txt not found, file not opened.\n");
head=NULL;
free(head);
free(empinfo);
return;
}
struct employeeData *tempPtr = head;
while (tempPtr->EMP_ID != 0){
fscanf(empinfo, "%d %s %d %d %lf", &tempPtr->EMP_ID, tempPtr->name, &tempPtr->dept, &tempPtr->rank, &tempPtr->salary);
if (tempPtr->EMP_ID == 0){
break;
}
tempPtr->next = (struct employeeData *)malloc(sizeof(struct employeeData));
tempPtr=tempPtr->next;
}
tempPtr=head;
while(tempPtr->next->EMP_ID!=0){
tempPtr=tempPtr->next;
}
empinfo=NULL;
free(empinfo);
tempPtr->next=NULL;
fclose(empinfo);
tempPtr=NULL;
free(tempPtr);
}
void add(int ID, char name[], int dept, int rank, double newSalary){
struct employeeData *tempPtr = head;
while ((tempPtr->next!=NULL) && (tempPtr->next->EMP_ID < ID)) {
tempPtr=tempPtr->next;
}
struct employeeData *newNode = (struct employeeData * )malloc(sizeof(struct employeeData));
newNode->EMP_ID = ID;
strcpy(newNode->name,name);
newNode->dept = dept;
newNode->rank = rank;
newNode->salary = newSalary;
newNode->next=NULL;
if (tempPtr==head) {
if(ID>tempPtr->EMP_ID){
newNode->next = tempPtr->next;
tempPtr->next = newNode;
}
else {
newNode->next=tempPtr;
head=newNode;
}
}
else if (tempPtr->next == NULL){
tempPtr->next=newNode;
newNode->next=NULL;
}
else{
newNode->next = tempPtr->next;
tempPtr->next = newNode;
}
printf("Employee #%d has been added to the database.\n\n", ID);
tempPtr=NULL;
newNode=NULL;
free(newNode);
free(tempPtr);
}
void deleteEmp(int ID){
struct employeeData *seek=head,*tempPtr = head;
if(head!=NULL) {
while((tempPtr->EMP_ID!=ID)){
seek = tempPtr;
tempPtr=tempPtr->next;
if(tempPtr==NULL){
printf("Employee ID not found\n");
free(tempPtr);
seek=NULL;
free(seek);
return;
}
}
if (tempPtr!=NULL){
if(tempPtr==head){
if(tempPtr->next==NULL){
printf("List cannot be empty\n");
free(tempPtr);
seek=NULL;
free(seek);
return;
}
head=tempPtr->next;
free(tempPtr);
}
else if (tempPtr->next==NULL){
free(tempPtr);
seek->next = NULL;
}
else{
seek->next=tempPtr->next;
free(tempPtr);
}
}
}
printf("Employee #%d has been deleted\n", ID);
tempPtr=NULL;
free(tempPtr);
seek=NULL;
free(seek);
}
void modify(int ID, double NewSalary){
struct employeeData *tempPtr = head;
while (tempPtr!=NULL&&tempPtr->EMP_ID!=ID){
tempPtr=tempPtr->next;
}
if(tempPtr==NULL){
printf("Employee ID not found\n");
free(tempPtr);
return;
}
tempPtr->salary=NewSalary;
printf("Employee salary updated.\n\n");
tempPtr=NULL;
free(tempPtr);
}
void query(int rank){
struct employeeData *tempPtr = head;
while (tempPtr!=NULL){
if(tempPtr->rank == rank){
printf("%s\n", tempPtr->name);
tempPtr=tempPtr->next;
}
else
tempPtr=tempPtr->next;
}
tempPtr=NULL;
free(tempPtr);
}
void print(){
struct employeeData *tempPtr = head;
while (tempPtr!=NULL){
printf("%d %s %d %d %.0lf\n", tempPtr->EMP_ID, tempPtr->name, tempPtr->dept, tempPtr->rank, tempPtr->salary);
tempPtr=tempPtr->next;
}
free(tempPtr);