简体   繁体   中英

How can I print out scanned in structure to file properly? (C)

I am trying to read from 2 files (f1.txt and f2.txt) using structures and print them out to the third file (f3.txt) , but I seem to be getting some errors. Issues did not occur before I tried printing out the values to the file (fprintf command) , and I cant seem to find a way to fix it..

This is for a homework assignment, but since I've been struggling to fix this issue for months now (yes, I'm pretty bad), I thought maybe anyone here knows how I can fix this.

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

I expected the program to output the information from f1.txt and f2.txt to f3.txt, but currently compiler tells me that I cannot do that, since I'm using * int in last function, but it says that regular int is required.

Compiler is right: andmed_failidesse expects an int as last parameter and you're passing Hinne which is an array of int , aka int* .

As Tud2 is not used anyway in your current code you can remove it from function signature or rework your function to use it. Your compiler should also warn you that you have other unused parameters in your function.

It is obviously a work in progress: take a break, re-read your C courses and try to figure out what your function is supposed to do, and which parameters it needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM