0

So all my functions are working properly except for my:

void printWorkers(int numOfWorkers);

function, which isn't executing, I thought I placed all the variables and declarations in the right place, so I'm stuck wondering what is preventing it from executing.

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>

#define MAX 100

FILE *fp;
FILE *fpIn;
FILE *fpOut;

typedef struct {
    char first[8];
    char initial[2];
    char last[11];
    char street[17];
    char city[12];
    char state[3];
    char zip[6];
    int age;
    char sex[2];
    int tenure;
    double salary;
    } payroll;

int readFile();
void strsub (char buf[], char sub[], int start, int end);
void printWorkers(int numOfWorkers);
payroll workers[MAX];

int main()
{
    int numOfWorkers = 0;


        if (!(fpIn = fopen("payfile.txt", "r")))
    {
        printf("payfile.txt could not be opened for input.");
        exit(1);
    }
    if (!(fpOut = fopen("csis.txt", "w")))
    {
        printf("csis.txt could not be opened for output.");
        exit(1);
    }

    readFile();

    numOfWorkers = readFile();

    printWorkers(numOfWorkers);

    return 0;
}


int readFile()
{
    int i = 0;
    char buf[MAX];
    while(!feof(fpIn))
    {

            fgets(buf, MAX, fpIn);
            strsub(buf, workers[i].first, 0, 6);
            strsub(buf, workers[i].initial, 8, 8);
            strsub(buf, workers[i].last, 9, 18);
            strsub(buf, workers[i].street, 19, 34);
            strsub(buf, workers[i].city, 36, 46);
            strsub(buf, workers[i].state, 49, 50);
            strsub(buf, workers[i].zip, 51, 56);
            sscanf(buf+58, "%2d", &workers[i].age);
            strsub(buf, workers[i].sex, 61, 61);
            sscanf(buf+63, "%d", &workers[i].tenure);
            sscanf(buf+65, "%lf", &workers[i].salary);

            ++i;

    }

    return i;
}

void strsub (char buf[], char sub[], int start, int end)
{
    int i, j;

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

void printWorkers(int numOfWorkers)
{
    int i;

    for (i = 0; i < numOfWorkers; ++i)
    {
        printf("%s", workers[i].first);
        printf("%s", workers[i].initial);
        printf("%s", workers[i].last);
        printf("%s", workers[i].street);
        printf("%s", workers[i].city);
        printf("%2s", workers[i].state);
        printf("%5s", workers[i].zip);
        printf("% 2d", workers[i].age);
        printf(" %1s", workers[i].sex);
        printf(" %1d", workers[i].tenure);
        printf(" %.2lf", workers[i].salary);
    }
}

2 Answers2

2

You're calling readFile twice:

readFile();

numOfWorkers = readFile();

The second time around it returns 0 since it has already reached EOF. And you're printWorkers uses 0 to iterate exactly 0 times, thus it looks as if it doesn't get called, even though it does.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
1
  1. readFile();

  2. numOfWorkers = readFile();

Remove the first one here.

µtex
  • 900
  • 5
  • 12