gpt4 book ai didi

c - 功能未正常执行

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:22 25 4
gpt4 key购买 nike

所以我的所有功能都正常工作,除了我的:

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);
}
}

最佳答案

你正在调用 readFile 两次:

readFile();

numOfWorkers = readFile();

第二次它返回 0,因为它已经到达 EOF。你是 printWorkers 使用 0 来精确迭代 0 次,因此它看起来好像没有被调用,即使它确实被调用了。

关于c - 功能未正常执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37917970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com