如何打印出扫描的结构以正确归档? (C)

时间:2019-04-17 06:07:28

标签: c pointers structure

我正在尝试使用structuresprint 2个文件(f1.txt和f2.txt)读取到第三个​​文件(f3 .txt),但是我似乎遇到了一些错误。在尝试将值打印到文件(fprintf command)之前没有出现问题,而且我似乎找不到解决此问题的方法。

这是一项家庭作业,但是由于我几个月来一直在努力解决此问题(是的,我非常糟糕),我想也许这里有人知道我该如何解决此问题。

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define MAX_NIME_PIKKUS 100
#define MAX_AINE_PIKKUS 100
#define MAX_KOOD 10
#define MAX_HINNE 5

#define DEBUG 0

    int n;
    int m;

struct Tudeng{
    char Nimi[MAX_NIME_PIKKUS];
    char Kood[MAX_KOOD];
};
struct Tudeng *pTudeng;

struct Aine{
    char Nimetus[MAX_AINE_PIKKUS];
    char aineKood[MAX_KOOD];
};
struct Aine *pAine;

struct Tud{
    char Tudengikood[MAX_KOOD];
    int Hinne[MAX_HINNE];
};
struct Tud *pTud;

char f1[] = "f1.txt";
char f2[] = "f2.txt";
char f3[] = "f3.txt";
FILE *fp1,*fp2,*fp3;

int sisendf1_kontroll();
int sisendf2_kontroll();
void tekita_failid();
void andmed_failidesse(char Tudeng1, char Tudeng2, char Aine1, char Aine2, char Tud1, int Tud2);

int main(void){
    int a;
    int b;
    int c;
    n = sisendf1_kontroll();
    printf("Failist %s loeti %d tudengi andmed.\n", f1, n);
    m = sisendf2_kontroll();
    printf("Failist %s loeti %d aine andmed.\n", f2, m);
    fp1 = fopen(f1,"r");
    fp2 = fopen(f2, "r");
    int i = 0;

    a = sizeof(struct Tudeng);
    b = sizeof(struct Aine);
    c = sizeof(struct Tud);

    pTudeng = malloc(a * n);
    pAine = malloc(b * m);
    pTud = malloc(c * m);

    if(DEBUG)printf("Struktuuri Tudeng baidi aadress on %p, ühe kirje andmeteks eraldati mälu %d baiti, mälu eraldati massiivile kokku %d baiti \n", pTudeng, a, a * n);
    if(DEBUG)printf("Struktuuri Aine baidi aadress on %p, ühe kirje andmeteks eraldati mälu %d baiti, mälu eraldati massiivile kokku %d baiti \n", pAine, b, b * m);
    if(DEBUG)printf("Struktuuri Tud baidi aadress on %p, ühe kirje andmeteks eraldati mälu %d baiti, mälu eraldati massiivile kokku %d baiti \n", pTud, c, c * m);
    int loopiks;

while(loopiks == 0){
    while(!feof(fp1)){
        fscanf(fp1,"%s",(pTudeng+i)->Nimi);
        fscanf(fp1,"%s",(pTudeng+i)->Kood);
        i++;
    }

    while(!feof(fp2)){
        fscanf(fp2,"%s",(pAine+i)->Nimetus);
        fscanf(fp2,"%s",(pAine+i)->aineKood);
        fscanf(fp2,"%s",(pTud+i)->Tudengikood);
        fscanf(fp2,"%d",(pTud+i)->Hinne);
        i++;
    }
    loopiks = 1;
    tekita_failid();
    andmed_failidesse((pTudeng+i->Nimi), (pTudeng+i)->Kood, (pAine+i)->Nimetus, (pAine+i)->aineKood, (pTud+i)->Tudengikood, (pTud+i)->Hinne);

    free(pTudeng);
    free(pAine);
    free(pTud);
}

    //fprintf(fp3, "%s %s\n",(pTudeng+i)->Nimi,(pTudeng+i)->Kood);

    fclose(fp1);
    fclose(fp2);
    return 0;    
}

int sisendf1_kontroll(void){
    char rida[122];
    int n = 0, p;
    fp1 = fopen(f1,"r");

    if(fp1 == NULL){
        printf("Sisendfaili %s avamine ebaonnestus!", f1);
        exit(1);
    }else{
        while(!feof(fp1)){
            fgets(rida, 122, fp1);
            p = strlen(rida);
            if (p > 1) n++;
        }
    }
    fclose(fp1);
    return n;
}

int sisendf2_kontroll(void){
    char rida2[122];
    int m = 0, o;
    fp2 = fopen(f2,"r");

    if(fp2==NULL){
        printf("Sisendfaili %s avamine ebaonnestus!", f2);
        exit(1);
    }else{
        while(!feof(fp2)){
            fgets(rida2, 122, fp2);
            o = strlen(rida2);
            if (o > 1) m++;
        }
    }
    fclose(fp2);
    return m;
}

void tekita_failid(){
    fp3 = fopen(f3, "w");
    fclose(fp3);
    return;
}

void andmed_failidesse(char Tudeng1, char Tudeng2, char Aine1, char Aine2, char Tud1, int Tud2){
    fp3 = fopen(f3, "a");
    int i;
    int j;
    while(i < n && j < m){
        for(i = 0; i < n; i++){
            fprintf(fp3, "%s %s ",(pTudeng+i)->Nimi,(pTudeng+i)->Kood);
        }
        for(j = 0; j < m; j++){
            fprintf(fp3, "%s %s %s %d \n",(pAine+i)->Nimetus,(pAine+i)->aineKood, (pTud+i)->Tudengikood, (pTud+i)->Hinne);
    }
}
        return;
}

我希望程序将 f1.txt和f2.txt 中的信息输出到 f3.txt ,但是当前编译器告诉我我不能这样做,因为我在上一个函数中使用了* int,但它表示需要常规的int。

1 个答案:

答案 0 :(得分:1)

编译器是正确的: andmed_failidesse期望将int作为最后一个参数,并且您要传递Hinne,它是int的数组,又名int*

由于在当前代码中始终未使用Tud2,因此您可以将其从函数签名中删除或重新编写函数以使用它。 编译器还应警告您函数中还有其他未使用的参数。

这显然是一项正在进行的工作:休息一下,重新阅读C课程,并尝试弄清楚您的函数应该做什么以及需要什么参数。