Home > Back-end >  Modify the code, to the operation of the file
Modify the code, to the operation of the file

Time:09-25

 
/* will student information read from the file */

Void IO_ReadInfo ()
{
FILE *fp;
int i;
If ((fp=fopen (" Database. TXT ", "rb"))==NULL)
{
Printf (" can't open the file! \n");
return;
}
If (fread (& amp; Num, sizeof (int), 1, fp)!
=1){
Num=1;
}
The else
{
for(i=0; i{
Fread (& amp; Students [I], sizeof (struct Student), 1, fp);
}
}
fclose(fp);
}



/* the students information written to the file */

Void IO_WriteInfo ()
{
FILE *fp;
int i;
If ((fp=fopen (" Database. TXT ", "wb"))==NULL)
{
Printf (" can't open the file! \n");
return;
}
If (fwrite (& amp; Num, sizeof (int), 1, fp)!
=1){
Printf (" written to the file error! \n");
}
for (i=0; i{
If (fwrite (& amp; Students [I], sizeof (struct Student), 1, fp)!
=1){
Printf (" written to the file error! \n");
}
}
fclose(fp);
}


For a great god help change in the input and output of a text file, not binary

CodePudding user response:

Less likely to file operations, and a great spirit

CodePudding user response:

The write and read it's not a problem, you should be somewhere else

CodePudding user response:

Correct, read
part of the modified
/* will student information read from the file */

Void IO_ReadInfo ()
{
FILE *fp;
int i;
If ((fp=fopen (" Database. TXT ", "rb"))==NULL)
{
Printf (" can't open the file! \n");
return;
}
If (fread (& amp; Num, sizeof (int), 1, fp)!
=1){
Num=0;
}
The else
{
for(i=0; i{
Fread (& amp; Students [I], sizeof (struct Student), 1, fp);
}
}
fclose(fp);
}


If it is 1, your subscript began from 1
  • Related