0

I need to solve a problem. I need to create two functions which scan user's input information and put in into structure array and then put all array to BIN file (with function fwrite()) and other function - read from BIN file with function fread(). You can see my code below. Problem is that I can write to file, but when I read I get filler array element and other element which are empty. How to get only filled struct array element?

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

typedef struct
{
    char Lesson[50];
    char TeachersName[50];
    char TeachersLastName[50];
    int Credits;
    int NumberOfStudents;
} School;


void ToFile (char *fileName);
void FromFile (char *FileName);


int main()
{
    char *fileName[]={"student.bin"};
    ToFile (*fileName);
    FromFile (*fileName);
    return 0;
}


void ToFile (char *fileName)
{
    int n, chars;

    FILE *fp;
    fp = fopen(fileName, "wb");

    School Info[20];

    if(fp == NULL)
    {
        printf("Error opening file\n");
        exit(1);
    }

    printf("Testing fwrite() function: \n\n");

    printf("Enter the number of records you want to enter: ");
    scanf("%d", &n);

    for(int i = 0;i<n;i++)
    {
        printf("\nEnter details of employee %d \n", i + 1);

        fflush(stdin);
        printf("Lesson: ");
        gets(Info[i].Lesson);
        
        printf("Teachers name: ");
        gets(Info[i].TeachersName);

        printf("Teachers last name: ");
        gets(Info[i].TeachersLastName);

        printf("Credits: ");
        scanf("%d", &Info[i].Credits);

        printf("Number of studens: ");
        scanf("%d", &Info[i].NumberOfStudents);

        chars = fwrite(&Info[i], sizeof(Info), 1, fp);
        printf("Number of items written to the file: %d\n", chars);
    }


    fclose(fp);

}


void FromFile (char *FileName)
{
    School Info[20]= { { "", "","",0,0 } };;
    FILE * fpp;
    fpp = fopen(FileName, "rb");


    fread(&Info, sizeof(Info), 1, fpp);
/*
    for(int j=0; j<20; ++j) {
        printf("\nLesson: %s", Info[j].Lesson);
        printf("\nTeachers name: %s", Info[j].TeachersName);
        printf("\nTeachers last name: %s", Info[j].TeachersLastName);
        printf("\nCredits: %d", Info[j].Credits);
        printf("\nNumber of students: %d", Info[j].NumberOfStudents);
        printf("\n");
    }*/


    int j=0;
    while (fread(&Info, sizeof(Info), 1, fpp))
    {

        printf("\nLesson: %s", Info[j].Lesson);
        printf("\nTeachers name: %s", Info[j].TeachersName);
        printf("\nTeachers last name: %s", Info[j].TeachersLastName);
        printf("\nCredits: %d", Info[j].Credits);
        printf("\nNumber of students: %d", Info[j].NumberOfStudents);
        printf("\n");
        j++;


    }

    fclose(fpp);
    
}
NewAtC
  • 55
  • 9
  • 1
    `fwrite(&Info[i], sizeof(Info), 1, fp)` should be `fwrite(&Info[i], sizeof(Info[i]), 1, fp)` – kaylum Dec 14 '20 at 10:25
  • 1
    Similarly, `fread(&Info, sizeof(Info), 1, fpp)` should be `read(&Info[j], sizeof(Info[j]), 1, fpp)` – kaylum Dec 14 '20 at 10:26
  • And there are N number of duplicates where they say [gets-function-so-dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used#:~:text=gets()%20is%20dangerous%20because,a%20seg%20fault%20and%20crash.) – IrAM Dec 14 '20 at 10:33
  • You don't need arrays of structures. A single structure object is all you need in both functions. – Some programmer dude Dec 14 '20 at 10:34
  • Also, never ***ever*** use `gets`! And note that passing an input-only stream (like `stdin`) to `fflush` is explicitly mentioned in the C specification as *undefined behavior*. – Some programmer dude Dec 14 '20 at 10:35
  • @Someprogrammerdude but I need to put all data to array to work with it later on console (edit array element or delete on of the element) – NewAtC Dec 14 '20 at 10:37
  • Then in `ToFile` you could initialize the array, and then write it all in one go: `fwrite(Info, sizeof Info[0], n, fp);` And in `FromFile` read all in one: `size_t n = fread(Info, sizeof Info[0], 20, fp);` – Some programmer dude Dec 14 '20 at 10:40

0 Answers0