Like the title says the portion of code
printf("%s", workers[0].first);
prints all the way up to the age part of the struct array, when I really just want it to print the "first" portion
in addition to that, I can't get the actual printWorkers() function to work (note that I have not set up the actual printing correctly inside that function, it is just a place holder)
this is the portion of .txt file it is reading
"ADA A AGUSTA 33 BABBAGE ROAD LOVELACE GB 19569 28 F 2 350.50"
The spaces don't appear the same in the above text as it does in the file.
The last thing I'm wondering is why a random character is being appended to the end of zip variable when it's printed out (not that I'm entirely sure it matters since it isn't supposed to print it)
It's also worth mentioning that because of my requirements the struct cannot be altered in any way.
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
FILE *fp;
FILE *fpIn;
FILE *fpOut;
typedef struct {
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;
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);
printf("%s", workers[0].first);
printf(" %d", workers[0].age);
printf(" %s", workers[0].sex);
printf(" %d", workers[0].tenure);
printf(" %.2lf", workers[0].salary);
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, 48, 49);
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("%7s %2s %10s %17s %12s %3s %6s %3d %2s %5d %.2lf\n",
workers[i].first, workers[i].initial, workers[i].last, workers[i].street, workers[i].city,
workers[i].state, workers[i].zip, workers[i].age, workers[i].sex, workers[i].tenure, workers[i].salary);
}
}