0

I'm trying to combine structures and files. So, I want to read n elements of the structure in the data file.txt., there can be N records, (in my case day, month and year), and then manipulate this data, and the result to enroll in another file. Could you help me with that? I'd be very grateful. Here's my code

#include <stdio.h>


struct data{
    int d, m, y;
};


void readN(struct data element[],int n){
    FILE *data_file;
    data_file = fopen("data.txt","r");
    if(data_file == NULL){
        fprintf(stderr, "\n Error!!!");
    }
    else{
        while(!feof(data_file)){
            fscanf(data_file,"%d %d %d", &element->d, &element->m, &element->y);
        }
    }
}


int compareDates (struct data d1, struct data d2)
{

    if (d1.y < d2.y)
        return -1;
    else if (d1.y > d2.y)
        return 1;


    else if (d1.m < d2.m)
        return -1;
    else if (d1.m > d2.m)
        return 1;


    else if (d1.d < d2.d)
        return -1;
    else if (d1.d > d2.d)
        return 1;

    else
        return  0;

}

struct data checkMax(struct data *element, int n){
    struct data max = element[0];
    int i;
    for (i = 0; i < n; i++){
        if(compareDates(max,element[i]) == -1){
            max = element[i];
        }
    }
    return max;


}

struct data checkMin(struct data *element, int n) {
    struct data min = element[0];
    int i;
    for (i = 0; i < n; i++){
        if(compareDates(min,element[i]) == 1){
            min = element[i];
        }
    }
    return min;
}

int checkLeapYear(struct data yMax, struct data yMin){
    int counter = 0;

    for(int i = yMin.y; i <= yMax.y; i++ ){
        if((i % 4 == 0 && i % 100 != 0) || i % 400 == 0){
            counter++;
        }
    }
    return counter;

}



int main() {

   struct data dd1[4];
    readN(dd1,4);

    struct data maximum = checkMax(dd1,4);
    struct data minimum = checkMin(dd1,4);

    printf("\n Data maxima %d %d %d",maximum.d,maximum.m,maximum.y);
    printf("\n Data minima %d %d %d", minimum.d,minimum.m,minimum.y);
    printf("\n Nr de ani bisecti :  %d ", checkLeapYear(maximum,minimum));

    return 0;
}

data.txt

10 10 2001
1 1  2002
14 3 2004
18 4 2022

My console

Data maxima 6422280 1986752301 4201664
 Data minima 1986776256 2133694049 -2
 Nr de ani bisecti :  1018905
 Error!!!
kammy
  • 93
  • 8
  • 1
    Please note that Stack Overflow is a place for asking **specific** programming questions. You may want to read this for further information: [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/12149471) – Andreas Wenzel Apr 18 '22 at 17:51
  • 2
    [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Apr 18 '22 at 17:51
  • `readN` never uses the `n` parameter. It always reads the entire file, even if that's more than `n` records. – Barmar Apr 18 '22 at 17:52
  • @Barmar how could I rewrite that? – kammy Apr 18 '22 at 17:53
  • 1
    you're also reading into the same element each time through the loop. You should use a `for` loop that increments an array index, and read into that element of the array. – Barmar Apr 18 '22 at 17:54
  • @AndreasWenzel I am a beginner and I have encountered problems in achieving this task, I can not formulate a specific question – kammy Apr 18 '22 at 17:54
  • @kammy: You may also want to read this: [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/12149471) Currently, you are only telling us which feature you want to implement, but you are not explaining which problem you encountered in your attempt. – Andreas Wenzel Apr 18 '22 at 18:02
  • @AndreasWenzel I'm sorry if I let that get away with it. I am not an English speaker and I find it difficult to express myself in English. – kammy Apr 18 '22 at 18:03

1 Answers1

0

readN() is only supposed to read n rows from the file, so it doesn't write outside the array. You can use a for loop to do this.

Your code was repeatedly reading into the first element of the array, since it never incremented element. You can use the iteration variable of the loop as the array index.

Inside the loop you should check the return value of fscanf() to make sure it was able to read all the items.

void readN(struct data element[],int n){
    FILE *data_file;
    data_file = fopen("data.txt","r");
    if(data_file == NULL){
        fprintf(stderr, "Unable to open file\n");
        return;
    }
    for (int i = 0; i < n; i++) {
        if (fscanf(data_file,"%d %d %d", &element[i].d, &element[i].m, &element[i].y) != 3){
            fprintf(stderr, "Incomplete input!!!\n");
            break;
        }
    }
    fclose(data_file)
}
Barmar
  • 741,623
  • 53
  • 500
  • 612