Thread: Storing values from a file into array of structs

  1. #1
    Registered User
    Join Date
    Mar 2012

    Storing values from a file into array of structs

    Hey everyone, I'm trying to store some information about different employees (from a text file 'payfile') into an array of structs. I wrote out some code and it seems to be working, but whenever I try outputting it, it prints more than what should theoretically be stored in each member of the struct.
    In the code below, the printf functions were just used to see if it was storing the information correctly...can you find anything wrong?
    EDIT: I'm guessing it has something to do with initializing the buf and temp arrays...

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 100
    struct employee {
        char first[7];
        char initial[1];
        char last[9];
        char street[16];
        char city[11];
        char state[2];
        char zip[5];
        int age;
        char sex[1];
        int tenure;
        float salary;
    } workers[MAX];
    char* strsub(char *buf, char *s, int start, int end) {
         int i, j;
         for (j=0, i=start; i<=end; i++, j++)
              s[j] = buf[i];
         s[j] = '\0';
         return s;
    main (void)
        FILE *payfile;
        FILE *csis;
        char buf[MAX] = {0};
        char temp[MAX] = {0};
        int i = 0;
        csis = fopen("csis.txt", "w");
        payfile = fopen("payfile.txt", "r");
        if (payfile == NULL){
            printf("Error: Could not open file");
    while (!feof(payfile)) {
         fgets(buf, MAX, payfile);
         strsub(buf, workers[i].first, 0, 6);
         strsub(buf, workers[i].initial, 8, 8);
         strsub(buf, workers[i].last, 10, 18);
         strsub(buf, workers[i].street, 20, 35);
         strsub(buf, workers[i].city, 37, 47);
         strsub(buf, workers[i].state, 49, 50);
         strsub(buf, workers[i].zip, 52, 56);
         strsub(buf, temp, 58, 59);
         workers[i].age = atoi(temp);
         strsub(buf, workers[i].sex, 61, 61);
         strsub(buf, temp, 63, 63);
         workers[i].tenure = atoi(temp);
         strsub(buf, temp, 65, 70);
         workers[i].salary = atof(temp);
            printf("%s", workers[0].first);
            printf("%s", workers[0].initial);
            printf("%s", workers[0].last);
            printf("%s", workers[0].street);
            printf("%s", workers[0].city);
            printf("%s", workers[0].state);
            printf("%s", workers[0].zip);
            printf("%d", workers[0].age);
            printf("%s", workers[0].sex);
            printf("%d", workers[0].tenure);
            printf("%f", workers[0].salary);
    return 0;
    Thanks a lot for any help!
    Last edited by ryanmcclure4; 05-11-2012 at 09:28 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Part of your problem may be caused by using freof() to control your loop. You will probably get one extra entry because freof() is not set until after you try to read past the end of file. So at the beginning of your loop freof() is not set but when you read the file with fgets() you are at the end of file and it fails to read any information but instead of exiting the loop you add another entry. Read the FAQ for how to fix this issue.

    Also main() should be defined to return an int, and you should return an int from this function. int main()


  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    None of your char arrays seem to be large enough.
    You can't have char[1] and expect to store any characters as well as a \0.

    Make each one something like char[50] just to see if your basic program logic is OK.
    Then worry about making it more efficient in memory space.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    A few things to fix

    EDIT: Aww, double-ninja'ed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-25-2012, 04:30 AM
  2. Storing values in an array
    By kpop in forum C Programming
    Replies: 3
    Last Post: 04-12-2011, 01:46 PM
  3. Replies: 12
    Last Post: 09-23-2010, 11:49 AM
  4. Storing Info From file into Array of structs
    By bigparker in forum C Programming
    Replies: 16
    Last Post: 06-27-2009, 02:21 AM
  5. Replies: 5
    Last Post: 10-02-2005, 12:15 AM