Problems with Polynomials loading from file.txt to terminal

57 Views Asked by At

I have problem with my function. Basically I need to load Polynomials from file.txt to terminal.

Basically, I have several coefficients on each row, and 1 polynomial equals 1 row. When I try my output on terminals is weird. 1 coefficient does not print and neither does 3 line/3 polynomial.

Here is my function:

polynom* vytvorZeSouboru(const char *soubor, int *pocet) {
    FILE *file = fopen(soubor, "r");

    if (file == NULL) {
        perror("Chyba pri otevirani souboru");
        exit(EXIT_FAILURE);
    }

    // Number of polynomials in the file - set to 0 at the beginning
    *pocet = 0;

    // Find out how many rows (polynomials) are in the file
    int c;
    while ((c = fgetc(file)) != EOF) {
        if (c == '\n') {
            (*pocet)++;
        }
    }

    // Let's go back to the beginning of the file
    rewind(file);

    // Pole polynomů
    Polynom *polePolynomu = (Polynom*)malloc(*pocet * sizeof(Polynom));

    //  Reading polynomials from a file
    for (int i = 0; i < *pocet; ++i) {
        int pocetKoeficientu;
        if (fscanf(file, "%d", &pocetKoeficientu) != 1) {
            fprintf(stderr, "Chyba pri cteni poctu koeficientu polynomu ze souboru.\n");
            exit(EXIT_FAILURE);
        }
       

        // Polynomial initialization
        polePolynomu[i].koeficienty = (int*)malloc(pocetKoeficientu * sizeof(int));
        polePolynomu[i].pocetKoeficientu = pocetKoeficientu;

        // Načtení koeficientů polynomu
        for (int j = 0; j < pocetKoeficientu; ++j) {
            if (fscanf(file, "%d", &polePolynomu[i].koeficienty[j]) != 1) {
                fprintf(stderr, "Chyba pri cteni koeficientu polynomu ze souboru.\n");
                exit(EXIT_FAILURE);
            }
        }
    }

    // Zavření souboru
    fclose(file);

    return polePolynomu;
}

void vypisPolynomy(const Polynom *polePolynomu, int pocet) {
    for (int i = 0; i < pocet; ++i) {
        printf("Polynom %d: ", i + 1);
        for (int j = 0; j < polePolynomu[i].pocetKoeficientu; ++j) {
            printf("%d ", polePolynomu[i].koeficienty[j]);
        }
        printf("\n");
    }
}

int main() {
    int pocet, i;
    
    const char *cestaKSouboru = "vytvorZeSouboru.txt"; 
     
    Polynom *polePolynomu = vytvorZeSouboru(cestaKSouboru, &pocet);
    
    vypisPolynomy(polePolynomu, pocet);

    // Memory release
    for (int i = 0; i < pocet; ++i) {
        free(polePolynomu[i].koeficienty);
    }
    free(polePolynomu);
}
2

There are 2 best solutions below

0
chux - Reinstate Monica On BEST ANSWER

Addressing one potential issue.

Line count

Code counts the number of '\n' to determine line count. This is off-by-one if the last line lacks a final '\n' before EOF.

Alternative, count the number of line begininngs.

*pocet = 0;
int prior = '\n';
int c;
while ((c = fgetc(file)) != EOF) {
  if (prior == '\n') {
    (*pocet)++;
  }
  prior = c;
}
1
ralf any On
//  Reading polynomials from a file
for (int i = 0; i < *pocet; ++i) {
    int pocetKoeficientu;

... } // further use of "pocetKoeficientu"

For later use the variable "int pocetKoeficientu;" is outside of scope (only valid within the for-loop). Put it at the beginning of the for-loop: // Reading polynomials from a file int pocetKoeficientu; for (int i = 0; i < *pocet; ++i) { ... } // further use of "pocetKoeficientu"