尝试阅读文本文件时遇到错误

时间:2014-11-16 22:08:39

标签: c function pointers data-structures text-files

好的,所以我正在构建一个工作程序,以便每天跟踪音量,并将音量分成每个不同位置接收的音量。大楼共有8条线路。文本文件包含当天的日期,建筑物的总体积和每个位置的体积(PEN)。

我现在遇到两个问题,它们都被隔离到我的readDay函数。其中result = fscanf(pi-> Date,40,fp); 问题是 1.不兼容的整数到指针转换将'int'传递给'const char *'类型的参数 2.不兼容的指针类型将'char [40]'传递给'FILE *'类型的参数(又名'struct_sFILE *')

任何人都可以向我解释这些问题的含义。或者,如果您需要更多信息,以帮助我随意提问。

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

struct  Day  {
char Date[40];
int Total;
int Pen1;
int Pen2;
int Pen3;
int Pen4;
int Pen5;
int Pen6;
int Pen7;
int Pen8;


};

const char fileName [] = "UPS.txt";

void StartRecord(void);
void printDay(struct Day* pi);
void writeDay(FILE *fp, struct Day *pi);
int readDay(FILE *fp, struct Day *pi);
void readFile(void);
void addDay(void);



int main(int argc, const char * argv[]) {

StartRecord();

int choice;
printf("Enter 1 to read this file or enter 2 to add to it.");
scanf("%d", &choice);
switch (choice) {
    case 1: readFile();
        break;
    default: printf("Not a valid choice\n");
        break;
}
getchar();
getchar();


}

void StartRecord(void)    {

int count;
int i;
struct Day aDay;
FILE *fp;

fp = fopen(fileName, "w");
if (fp != NULL) {
    printf("Enter volume for how many days? ");
    scanf("%d", &count);
    for (i = 0; i < count; ++i) {
        printf("What is today's date? ");
        gets(aDay.Date);
        printf("What was the total volume for the building for the day?");
        scanf("%d", &aDay.Total);
        printf("What was the volume for Pen 1? ");
        scanf("%d", &aDay.Pen1);
        printf("What was the volume for Pen 2? ");
        scanf("%d", &aDay.Pen2);
        printf("What was the volume for Pen 3? ");
        scanf("%d", &aDay.Pen3);
        printf("What was the volume for Pen 4? ");
        scanf("%d", &aDay.Pen4);
        printf("What was the volume for Pen 5? ");
        scanf("%d", &aDay.Pen5);
        printf("What was the volume for Pen 6? ");
        scanf("%d", &aDay.Pen6);
        printf("What was the volume for Pen 7? ");
        scanf("%d", &aDay.Pen7);
        printf("What was the volume for Pen 8? ");
        scanf("%d", &aDay.Pen8);
        getchar();
        writeDay(fp, &aDay);
    }
    fclose(fp);
}else {
    printf("\nError opening file.\n");
}

}

void readFile(void) {
FILE *fp;
struct Day aDay;

fp = fopen(fileName, "r");
if (fp != NULL) {
    while (readDay(fp, &aDay)) {
        printDay(&aDay);
    }
    fclose(fp);
} else  {
    printf("\nError opening File!\n");
}
}

void writeDay(FILE *fp, struct Day *pi) {
fprintf(fp, "%s\n", pi->Date);
fprintf(fp, "%d\n", pi->Total);
fprintf(fp, "%d\n", pi->Pen1);
fprintf(fp, "%d\n", pi->Pen2);
fprintf(fp, "%d\n", pi->Pen3);
fprintf(fp, "%d\n", pi->Pen4);
fprintf(fp, "%d\n", pi->Pen5);
fprintf(fp, "%d\n", pi->Pen6);
fprintf(fp, "%d\n", pi->Pen7);
fprintf(fp, "%d\n", pi->Pen8);
}

int readDay(FILE *fp, struct Day *pi)  {
int result;

fgetc(fp);    // read the endline
result = fscanf(pi->Date, 40, fp);
if (result == EOF) {
    return 0;
}
fscanf(fp, "%d", &pi->Total);
fscanf(fp, "%d", &pi->Pen1);
return 1;

}

void PrintDay(struct Day *pi)  {
printf("Records for the day of: %s\n", pi->Date);
printf("Total Volume: %d", pi->Total);
printf("Volume for Pen 1: %d", pi->Pen1);
printf("Volume for Pen 2: %d", pi->Pen2);
printf("Volume for Pen 3: %d", pi->Pen3);
printf("Volume for Pen 4: %d", pi->Pen4);
printf("Volume for Pen 5: %d", pi->Pen5);
printf("Volume for Pen 6: %d", pi->Pen6);
printf("Volume for Pen 7: %d", pi->Pen7);
printf("Volume for Pen 8: %d", pi->Pen8);
printf("\n\n");

}

1 个答案:

答案 0 :(得分:0)

从某种意义上说,你的论点是错误的。这样:

fscanf(pi->Date, 40, fp);

应该是:

fscanf(fp, pi->Date, 40);

就像你已经进一步向下几行一样。

从另一个意义上说,你要做的事情非常奇怪 - 你无法将任何东西读成字面整数。你可能想要更像的东西:

fscanf(fp, "%s", pi->Date);

或可能:

fscanf(fp, "%39s", pi->Date);

或类似。