S
Simon Mansfield
Im trying to read in a list of employee's from a file, in the format:
01:SJMrogrammer:481 (<- £4.81 an hr)
02:AMGython Scripter:512
:
:
etc.
When assigning to the "new" pointer it brings up an assignment error on line
33 and 34 of Staff.c, now these are the two string fields.. But I dont see
why it is doing it!? Anyone?
So I have created this file Staff.c:
#include "main.h"
struct employee {
int pid;
char name[3];
char job[40];
int hourlyRate;
char *appointments[7];
struct employee *next;
};
PTR read_in_staff() {
// Create head and new pointers.
PTR head = NULL;
PTR new = NULL;
FILE *input; // File to read employee's from.
char filename[40]; // String to store file path.
PERSON store; // A PERSON struct to store the read in data.
// Prompt user for file path.
printf("\nPlease enter the path of the input file: ");
gets(filename);
if((input = fopen(filename, "r")) != NULL) { //Opens the file in read
mode.
while(feof(input) == 0) { // While the end of file has not been
reached.
fscanf(input, "%d:%s:%s:%d", store.pid, store.name, store.job,
store.hourlyRate); // Get the employee fields and put in "store" struct.
new = (PTR)malloc(sizeof(PERSON)); // Create an instance of struct
PERSON and allocate space.
new->next = head; // Make the new node link to the first node.
head = new; // Make head now point to the new element.
new->pid = store.pid; // Fill values with data.
new->name = store.name;
new->job = store.job;
new->hourlyRate = store.hourlyRate;
}
} else {
printf("Error opening this file"); // Error produced on incorrect file
entry.
}
return head;
}
And a header file Staff.h:
/* staff.h - Header file for staff.c */
#ifndef _STAFF_H
#define _STAFF_H 1
/* The function prototypes */
typedef struct employee PERSON;
typedef PERSON *PTR;
PTR read_in_staff();
#endif
01:SJMrogrammer:481 (<- £4.81 an hr)
02:AMGython Scripter:512
:
:
etc.
When assigning to the "new" pointer it brings up an assignment error on line
33 and 34 of Staff.c, now these are the two string fields.. But I dont see
why it is doing it!? Anyone?
So I have created this file Staff.c:
#include "main.h"
struct employee {
int pid;
char name[3];
char job[40];
int hourlyRate;
char *appointments[7];
struct employee *next;
};
PTR read_in_staff() {
// Create head and new pointers.
PTR head = NULL;
PTR new = NULL;
FILE *input; // File to read employee's from.
char filename[40]; // String to store file path.
PERSON store; // A PERSON struct to store the read in data.
// Prompt user for file path.
printf("\nPlease enter the path of the input file: ");
gets(filename);
if((input = fopen(filename, "r")) != NULL) { //Opens the file in read
mode.
while(feof(input) == 0) { // While the end of file has not been
reached.
fscanf(input, "%d:%s:%s:%d", store.pid, store.name, store.job,
store.hourlyRate); // Get the employee fields and put in "store" struct.
new = (PTR)malloc(sizeof(PERSON)); // Create an instance of struct
PERSON and allocate space.
new->next = head; // Make the new node link to the first node.
head = new; // Make head now point to the new element.
new->pid = store.pid; // Fill values with data.
new->name = store.name;
new->job = store.job;
new->hourlyRate = store.hourlyRate;
}
} else {
printf("Error opening this file"); // Error produced on incorrect file
entry.
}
return head;
}
And a header file Staff.h:
/* staff.h - Header file for staff.c */
#ifndef _STAFF_H
#define _STAFF_H 1
/* The function prototypes */
typedef struct employee PERSON;
typedef PERSON *PTR;
PTR read_in_staff();
#endif