Thread: How to load array with text from a file?

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    7

    How to load array with text from a file?

    I am trying to load my array from text read from a file. My text file looks like this:
    a 50.00 2.00
    b 40.00 2.00
    c 40.00 2.00
    d 50.00 2.00
    e 40.00 2.00
    My function to read the data works fine and reads and prints it all correctly, but when I ask to print a specific array in my main it puts out garbage. Can someone tell me how to load an array from a file?
    This is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    //structure for employee data
    struct edata {
        char name[50];
        float hours;
        float rate;
    };
    //prototype
    int printdata(struct edata * print);
    //function for employee data
    //pass structure to function by reference
    int emplydata(struct edata * data)
    {
    
    
        printf("Enter employee name:\n");
        scanf_s("%s", &data->name, 20);
        if (strcmp(data->name, "-1") == 0)
            return -1;
    
    
        printf("Enter hours worked:\n");
        scanf_s("%f", &data->hours);
        if (data->hours == -1)
            return -1;
    
    
        printf("Enter hourly rate:\n");
        scanf_s("%f", &data->rate);
        if (data->rate == -1)
            return -1;
    
    
    }
    //funtion to calculate gross, base, and overtime pay
    
    
    int paycalc(struct edata * gr)
    {
    
    
    
    
        float overpay;
        float gross;
        float basepay;
        float overtime = 1.5;
        //variables hr and rt have the value of pointers
    
    
        if (gr->hours > 40) {
            basepay = (40 * gr->rate);
            gross = (40 * gr->rate) + ((gr->hours - 40)*(gr->rate * overtime));
            overpay = (gr->hours - 40)*(gr->rate * overtime);
            printf("Gross pay: %.2lf\n", gross);
            printf("Base pay: %.2lf\n", basepay);
            printf("Overtime pay: %.2lf\n", overpay);
        }
        else {
            basepay = (gr->hours * gr->rate);
            gross = basepay;
            overpay = 0;
            printf("Gross pay: %.2lf\n", gross);
            printf("Base pay: %.2lf\n", basepay);
            printf("Overtime pay: %.2lf\n", overpay);
        }
        //returns the value of gross pay
        return gross;
    }
    
    
    //function to calculate taxes and pay after tax is taken from gross
    int taxcalc(struct edata * tax, float g)
    {
        float taxpay;
        float taxrate = .2;
        if (tax->hours > 40) {
            taxpay = taxrate * g;
            printf("Tax pay: %.2lf\n", taxpay);
        }
        else {
            taxpay = taxrate * g;
            printf("Tax pay: %.2lf\n", taxpay);
        }
        return taxpay;
    }
    int fileinput(struct edata * fileemply, float gr, float * tg)
    {
        FILE * userfile;
        char chfl[50];
        errno_t err;
        printf("Which file would you like to load from?\n");
        scanf_s("%s", &chfl, 50);
        err = fopen_s(&userfile, chfl, "r+");
        if (err != 0)
        {
            printf("Error\n");
            return -1;
        }
        do {
            fscanf_s(userfile, " %s", fileemply->name, 50);
            fscanf_s(userfile, " %f", &fileemply->hours);
            fscanf_s(userfile, " %f", &fileemply->rate);
            printf(" %s", fileemply->name);
            printf(" %f", fileemply->hours);
            printf(" %f", fileemply->rate);
            gr = paycalc(fileemply);
            taxcalc(fileemply, gr);
            *tg += gr;
    
    
        } while (!feof(userfile));
        fclose(userfile);
    }
    
    
    int keyboard(struct edata * keyemply, float gr, float * tg)
    {
        emplydata(keyemply);
        gr = (paycalc(keyemply));
        taxcalc(keyemply, gr);
        *tg += gr;
    }
    
    
    int main(void)
    {
        FILE * emplyfile;
        //variables
        //array 6 so five employees are still listed while skipping array 0
        struct edata emply[10];
        int i;
        int menu;
        float totalgross = 0;
        float gross = 0;
        int editnum;
        int input;
        char answer;
        char filename[20];
    
    
        printf("Method of input:\n");
        printf("Would you like to load from a file or input from keyboard?\n");
        printf("1. Keyboard\n");
        printf("2. File\n");
        scanf_s("%d", &input);
        switch (input)
        {
        case 1:
            for (i = 1; i < 6; i++) {
                keyboard(&emply[i], gross, &totalgross);
            }
            printf("Net pay of all  employees is: %.2f\n", totalgross);
            break;
        case 2:
            fileinput(&emply, gross, &totalgross);
            printf("Net pay of all  employees is: %.2f\n", totalgross);
            break;
        }
        do {
            printf("Menu:\n");
            printf("1. Update an employee's data\n");
            printf("2. Print an employee's data\n");
            printf("3. Print all employee's data\n");
            printf("4. Exit program\n");
            printf("What would you like to do?\n");
            scanf_s("%d", &menu);
            printf("\n");
            //menu switch to choose options
            switch (menu)
            {
                //edit a single employee's data by manipulating array using load employee data function
            case 1:
                printf("Which employee record would you like to edit?\n");
                printf("1. %s\n", emply[1].name);
                printf("2. %s\n", emply[2].name);
                printf("3. %s\n", emply[3].name);
                printf("4. %s\n", emply[4].name);
                printf("5. %s\n", emply[5].name);
                scanf_s("%d", &editnum);
                emplydata(&emply[editnum]);
                break;
                //print single employee data through array editnum
            case 2:
                printf("Which employee record would you like to print?\n");
                printf("1. %s\n", emply[1].name);
                printf("2. %s\n", emply[2].name);
                printf("3. %s\n", emply[3].name);
                printf("4. %s\n", emply[4].name);
                printf("5. %s\n", emply[5].name);
                scanf_s("%d", &editnum);
                //printdata(emply[i].name);
                printdata(&emply[editnum]);
                break;
                //print all employee data
            case 3:
                for (i = 1; i < 6; i++) {
                    printdata(&emply[i]);
                };
                break;
                //exit program
            case 4:
                //ask to save file
                printf("Exiting program...\n");
                break;
            }
        } while (menu != 4);
        system("pause");
        return 0;
    }
    //function to print all employee data
    int printdata(struct edata * print)
    {
        float overpay;
        float gross;
        float basepay;
        float taxpay;
        float overtime = 1.5;
        float taxrate = .2;
        printf("Employee name: %s\n", print->name);
        printf("Employee hours: %.2lf\n", print->hours);
        printf("Employe rate: %.2lf\n", print->rate);
        if (print->hours > 40) {
            basepay = (40 * print->rate);
            gross = (40 * print->rate) + ((print->hours - 40)*(print->rate * overtime));
            overpay = (print->hours - 40)*(print->rate * overtime);
            printf("Gross pay: %.2lf\n", gross);
            printf("Base pay: %.2lf\n", basepay);
            printf("Overtime pay: %.2lf\n", overpay);
        }
        else {
            basepay = (print->hours * print->rate);
            gross = basepay;
            overpay = 0;
            printf("Gross pay: %.2lf\n", gross);
            printf("Base pay: %.2lf\n", basepay);
            printf("Overtime pay: %.2lf\n", overpay);
        }
        if (print->hours > 40) {
            taxpay = taxrate * gross;
            printf("Tax pay: %.2lf\n", taxpay);
        }
        else {
            taxpay = taxrate * gross;
            printf("Tax pay: %.2lf\n", taxpay);
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Compile with maximum warnings. This will help you find several mistakes in the code.

    Code:
    printf("1. %s\n", emply[1].name);
    printf("2. %s\n", emply[2].name);
    printf("3. %s\n", emply[3].name);
    printf("4. %s\n", emply[4].name);
    printf("5. %s\n", emply[5].name);
    Arrays start at zero, and go to "size - 1". Also, you should print exactly how many records were read - if more than five are read, this will not display them all, and if less than five are read, you will be printing uninitialized data.

    Now look at your "fileinput()" function. You want to update an array of struct, but you're not indexing different elements ... you're just overwriting the first element (element 0) over and over. Coupled with the above observation (printing starting at element 1 instead of 0), you will never see valid data in your print statements.

    Code:
    while (!feof(userfile))
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to load numbers from a text file?
    By Mexicouger in forum C Programming
    Replies: 7
    Last Post: 05-31-2011, 09:27 PM
  2. Load info form txt file into array
    By XodoX in forum C++ Programming
    Replies: 1
    Last Post: 10-05-2010, 11:38 AM
  3. I'd like to load a text file's contents cleanly at compile time
    By CharlieLewaska in forum C Programming
    Replies: 3
    Last Post: 01-31-2006, 11:10 AM
  4. Best way to load lines of text from a file to a combo box
    By ganonl in forum Windows Programming
    Replies: 6
    Last Post: 04-15-2004, 11:07 AM
  5. Load text dynamically in straight HTML?
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-24-2003, 06:52 AM

Tags for this Thread