我正在尝试让我的代码用文件中的数据填充结构中的数组。
截至目前,它还没有编译,只是给出了标题中的错误。
我希望能够解决这个问题,以便继续研究代码,但最近4小时对我没有任何作用......
#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;
} workers;
void readFile();
void strsub (char buf[], char sub[], int start, int end);
int main()
{
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);
}
return 0;
}
void readFile()
{
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, 10, 18);
strsub(buf, workers[i].street, 20, 35);
strsub(buf, workers[i].city, 37, 47);
strsub(buf, workers[i].state, 49, 50);
strsub(buf, workers[i].zip, 52, 56);
strsub(buf, workers[i].age, 58, 59);
strsub(buf, workers[i].sex, 61, 61);
strsub(buf, workers[i].tenure, 63, 63);
strsub(buf, workers[i].salary, 65, 70);
}
}
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';
}
答案 0 :(得分:0)
我修复了你的程序编译,但它仍然有问题。我不得不评论一个类型不匹配,但至少这个编译,以便你有一些工作。
#include <stdio.h>
#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;
} workers;
void readFile();
void strsub (char buf[], char sub[], int start, int end);
int main()
{
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);
}
return 0;
}
workers array[8];
void readFile()
{
int i =0;
char buf[MAX];
while(!feof(fpIn))
{
fgets(buf, MAX, fpIn);
strsub(buf, array[i].first, 0, 6);
strsub(buf, array[i].initial, 8, 8);
strsub(buf, array[i].last, 10, 18);
strsub(buf, array[i].street, 20, 35);
strsub(buf, array[i].city, 37, 47);
strsub(buf, array[i].state, 49, 50);
strsub(buf, array[i].zip, 52, 56);
strsub(buf, array[i].age, 58, 59);
strsub(buf, array[i].sex, 61, 61);
strsub(buf, array[i].tenure, 63, 63);
//strsub(buf, array[i].salary, 65, 70);
}
}
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';
}