Home > Software engineering >  Why the third entry student achievement, the program to stop working?
Why the third entry student achievement, the program to stop working?

Time:10-05

 # include 
#include
Typedef struct
{
Long ID, Class, Date;
Char Name [20].
Char gender [20].
Int Chinese, math, English, total;
} student;
Student s [2000];
Int num=0;
Void the add ()
{
Char ch, k [80];
The FILE * fp.
Fp=fopen (" sudent. TXT ", "a +");
if(! Fp)
{
Printf (" file connot be the opened ");
exit(1);
}
Printf (" input student data \ n ");
While (1)
{
Printf (" student id \ n ");
Gets (k);
S [num] ID=atol (k);
Printf (" name \ n ");
Gets (s) [r]. Num Name);
Printf (" gender \ n ");
Gets (s) [r]. Num gender);
Printf (" class \ n ");
Gets (k);
S [num]. Class=atol (k);
num++;
Fwrite (& amp; S (num), sizeof (s), num, fp);
Printf (" another (y/n)?" );
ch=getchar();
If (ch=='n')
break;
Gets (k);
The fflush (stdin);
}
fclose(fp);
}
Void main ()
{
The add ();
}

CodePudding user response:

num++;
Fwrite (& amp; S (num), sizeof (s), num, fp);

Should
Fwrite (& amp; S (num), sizeof (s), num, fp);
num++;
!

CodePudding user response:

Seems to be no effect

CodePudding user response:

Before each entry empty data?
You apply for the
Char Name [20].
Char gender [20].
Only 20 data area,
If not empty, inputting several times will be area of more than 20

CodePudding user response:

Your own Debug step through it and see, all the value of the parameter is correct?

CodePudding user response:

 
Typedef struct
{
Long ID, Class, Date;
Char Name [20].
Char gender [20].
Int Chinese, math, English, total;
} student;
Student s [2000];
Int num=0;
Void the add ()
{
Char ch, k [80];
The FILE * fp.
Fp=fopen (" sudent. TXT ", "a +");
if(! Fp)
{
Printf (" file connot be the opened ");
exit(1);
}
Printf (" input student data \ n ");
While (1)
{
Printf (" student id \ n ");
Gets (k);
S [num] ID=atol (k);
Printf (" name \ n ");
Gets (s) [r]. Num Name);
Printf (" gender \ n ");
Gets (s) [r]. Num gender);
Printf (" class \ n ");
Gets (k);
S [num]. Class=atol (k);
Fwrite (& amp; S (num), sizeof (student), num, fp);
num++;
Printf (" another (y/n)?" );
ch=getchar();
If (ch=='n') break;
The fflush (stdin);
}
fclose(fp);
}
Void main ()
{
The add ();
}

Note that
fwrite (& amp; S (num), sizeof (student), num, fp);
num++;

CodePudding user response:

Int len=sizeof (s);//136000
Len=sizeof (student);//68
The word
Fwrite (& amp; S (num), sizeof (s), num + 1, fp);
Only the num==0

CodePudding user response:

Don't spill!
  • Related