Files in C, access to pointers, reading and writing to files

I am trying to make a C program that keeps track of books borrowed by a student. It's hard for me to find pointers with files. When I use files, I usually do not use fscanf (), instead I use regular scanf. I have this data structure:

typedef struct{
   char fName[24], mInitial, lName[16];
}nameType;

typedef struct{
   unsigned long idNo;
   nameType studName;
   char course[8];
   int yrLevel;
   books borrowedBooks;
   int bksCtr;
}student;

typedef struct{
   student *studs;
   int studCtr;
}studList;

At the moment, I have created two functions: addStudToFile (void), which adds students to the file, and displayStudsFromFile (void), which basically prints students who were added to the file. This is my newbie function code:

void addStudToFile(void)
{

   FILE *fp;
   studList myStud;


   fp = fopen("students.db", "w");
   if(fp!=NULL){
      /* ask for student details and adds these to the file */
      printf("Enter ID number: ");
      fflush(stdin);
      scanf(,"%lu", &myStud.studs->idNo);
      printf("Enter First Name: ");
      fflush(stdin);
      gets(myStud.studs->studName.fName);
      printf("Enter Last Name: ");
      fflush(stdin);
      gets(myStud.studs->studName.lName);
      printf("Enter Middle Initial: ");
      fflush(stdin);
      scanf("%c", &(myStud.studs->studName.mInitial));
      printf("Enter Course: ");
      fflush(stdin);
      gets(myStud.studs->course);
      printf("Enter Year: ");
      fflush(stdin);
      scanf("%d", &(myStud.studs->yrLevel));
      fwrite(&myStud, sizeof(studList),1,fp);
      fclose(fp);
   }
}

and

void displayStudsFromFile(void)
{

   FILE *fp;
   studList myStud;

   fp = fopen("students.db", "r");
   if(fp!=NULL){
       while (fread(&myStud, sizeof(studList), 1, fp)){
           printf("%lu\t %s, %s %s\t %s-%d", myStud.studs->idNo, myStud.studs->studName.lName,
                                             myStud.studs->studName.fName, myStud.studs->studName.mInitial,
                                             myStud.studs->course, myStud.studs->yrLevel);
           printf("borrowed %d books", myStud.studs->bksCtr);
       }  
       fclose(fp);
   }
}

, myStud. addStudToFile() , , . ? - malloc? scanf() ? , , , - . -, / .

, :

enter image description here

:

enter image description here

, - . !

+4
5

, - malloc:)

typedef struct{
   student *studs;
   int studCtr;
}studList;

. , , → .

, , ,

student studs[10];

10 , addStudToFile() , . , , :

myStud.studs = (student *) malloc( sizeof(student) * how_many );

, , , , .

edit: malloc(), addStudToFile() - ,

free(myStud.studs);

...

, , fwrite() , , malloc() . sizeof (studlist) , . , , .

, , () studCtr, , malloced .

displayStudsFromFile(), , ,

student myStud;

.. , . fread() studCtr , fread() - myStud. :

printf("borrowed %d books", myStud.bksCtr);

, ... C : D

+3

myStud.studs , , . malloc , , &myStud.studs->idNo.

+1

, , .

, , .

:

  • .
  • .
  • .
0
along with the problems mentioned already,
this function has its' own set of troubles.
I have inserted '<--' and a comment at each problem 

fflush(stdin) though works on some implementations, it still undefined behaviour. 
According to the standard, fflush only works with output/update streams
( for your code, since the printf format strings do not end in '\n'
(  which would have forced the actual output to occur
(  change these lines to 'fflush(stdout)'

A ' ' in a scanf() format string will consume any white space found at that
point in the input.  Therefore, for almost all cases, the first char in
the format string should be: ' '.  Then newlines, spaces, etc 
will be consumed, as if they were never there.  It is even correct to 
use the leading ' ' when there is no white space to consume.

gets() is depreciated and will corrupt/overrun a input buffer, so NEVER 
use gets, rather, use fgets(), where the amount of input can be limited
and similar good things.

void addStudToFile(void)
{

   FILE *fp;
   studList myStud;


   fp = fopen("students.db", "w");
   if(fp!=NULL)
   {
      /* ask for student details and adds these to the file */

      printf("Enter ID number: ");
      fflush(stdin);  <-- change to stdout
      scanf(,"%lu", &myStud.studs->idNo); 
      <-- change format string to: " %lu"
      <-- add check of returned value to assure operation successful

      printf("Enter First Name: ");
      fflush(stdin); <-- change to stdout
      gets(myStud.studs->studName.fName); 
      <-- replace gets with fgets() +appropriate parms)
      <-- add check of returned value to assure operation successful

      printf("Enter Last Name: ");
      fflush(stdin); <-- change to stdout
      gets(myStud.studs->studName.lName); 
      <-- replace gets with fgets() +appropriate parms)
      <-- add check of returned value to assure operation successful

      printf("Enter Middle Initial: ");
      fflush(stdin); <-- change to stdout
      scanf("%c", &(myStud.studs->studName.mInitial)); 
      <-- replace format string with " %c"
      <-- add check of returned value to assure operation successful

      printf("Enter Course: ");
      fflush(stdin); <-- change to stdout
      gets(myStud.studs->course);
      <-- replace gets with fgets() +appropriate parms
      <-- add check of returned value to assure operation successful

      printf("Enter Year: ");
      fflush(stdin); <-- change to stdout
      scanf("%d", &(myStud.studs->yrLevel));
      <-- change format string to: " %d"
      <-- add check of returned value to assure operation successful

      fwrite(&myStud, sizeof(studList),1,fp);
      <-- add check of returned value to assure operation successful

      fclose(fp);
   <-- add else clause so use knows what happened. I.E.
       } else { perror( "fopen failed for write");  exit(EXIT_FAILURE); 
   } // end if
} // end function: addStudToFile
0
Here are my comments, prefixed by '<--'



void displayStudsFromFile(void)
{

   FILE *fp;
   studList myStud;

   fp = fopen("students.db", "r");
   if(fp!=NULL)
   {
       while (fread(&myStud, sizeof(studList), 1, fp))
       <-- add check of returned value to assure operation successful

       {
           printf("%lu\t %s, %s %s\t %s-%d", 
                  myStud.studs->idNo, 
                  myStud.studs->studName.lName,
                  myStud.studs->studName.fName, 
                  myStud.studs->studName.mInitial,
                  myStud.studs->course, 
                  myStud.studs->yrLevel);
           printf("borrowed %d books", myStud.studs->bksCtr);
       }  
       fclose(fp);
   <-- to let user know about error
   <-- insert: }else{ perror( "fopen failed for read"); exit(EXIT_FAILURE); 
   } // end if
} // end function: displayStudsFromFile
0
source

All Articles