0

The line that has been commented out in createStructs() is giving me the error in question. I am trying to scan in a list of information from a textfile and it is giving me a program crash after loading the file. When it crashes it loads out the output, but in a really weird manner. Here is my code:

#define MAX 71
#define SIZE 14

typedef struct {
    char first[7]; 
    char initial[1]; 
    char last[9]; 
    char street[16]; 
    char city[11];
    char state[2]; 
    int zipcode[5]; 
    int age; 
    char sex[1]; 
    int tenure; 
    double salary;
} Employee;
    insert main method just calling the functions
    void strsub(char buf[], char sub[], int start, int end) {
    int i, j = 0;

    for (j = 0, i = start; i <= end; i++, j++) {
        sub[j] = buf[i];
    }
    sub[j] = '\0';
}

void createStructs(Employee inWorkers[]) {
    int i = 0;
    char buf[MAX];
    char temp[SIZE];
    if (!(fp = fopen("payfile.txt", "r"))) {
        printf("payfile.txt could not be opened for input.");
        exit(1);
    }

    while (!(feof(fp))) {
        fgets(buf, MAX, fp);
        strsub(buf, inWorkers[i].first, 0, 6);
        strsub(buf, inWorkers[i].initial, 8, 8);
        strsub(buf, inWorkers[i].last, 10, 18);
        strsub(buf, inWorkers[i].street, 20, 35);
        strsub(buf, inWorkers[i].city, 37, 47);
        strsub(buf, inWorkers[i].state, 49, 50);
        strsub(buf, temp, 52, 56);
        //inWorkers[i].zipcode = atoi(temp);
        strsub(buf, temp, 58, 59);
        inWorkers[i].age = atoi(temp);
        strsub(buf, inWorkers[i].sex, 61, 61);
        strsub(buf, temp, 63, 63);
        inWorkers[i].tenure = atoi(temp);
        strsub(buf, temp, 65, 70);
        inWorkers[i].salary = atof(temp);
        i++;
    }
}

void printNames(Employee workers[]) {
    for (int i = 0; i < SIZE; i++) {
        printf("%s %s %s %s %s %s %d %d %s %d %.2lf", workers[i].first, workers[i].initial, workers[i].last,
            workers[i].street, workers[i].city, workers[i].state, workers[i].zipcode, workers[i].age,
            workers[i].sex, workers[i].tenure, workers[i].salary);
    }
}
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
  • 1
    Change `int zipcode[5];` to `int zipcode;` (unless you really need 5 zipcodes per employee). You also need to declare `FILE *fp = NULL;` before you attempt `fopen`. And see [**Why is “while ( !feof (file) )” always wrong?**](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – David C. Rankin May 06 '15 at 02:12

1 Answers1

1

Your problem is that you are creating an array of integers (int zipcode[5]). You then attempt to pass inworkers[i].zipcode the result of an atoi call. What this results in is you assigning an integer value to an integer pointer.

I'd be quite surprised if you are not receiving warnings at compile time. You really should compile with -Wall and resolve all warnings as a first step.

Rather than creating this is an array of integers, why not just let it be an integer value?

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67