所以我的所有功能都正常工作,除了我的:
void printWorkers(int numOfWorkers);
函数,它没有执行,我以为我把所有变量和声明放在正确的位置,所以我不知道是什么阻止它执行。
#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);
}
}
答案 0 :(得分:2)
您正在拨打readFile
两次:
readFile();
numOfWorkers = readFile();
第二次绕过它会返回0,因为它已经达到了EOF。并且你printWorkers
使用0来完全迭代0次,因此它看起来好像没有被调用,即使它被调用。
答案 1 :(得分:1)
READFILE();
numOfWorkers = readFile();
在这里删除第一个。