Home > Back-end >  Small white for help after the C language to read in the structure of the contents of the documents,
Small white for help after the C language to read in the structure of the contents of the documents,

Time:09-15

C
Will read in the structure of the contents of the documents, to structure the body data processing, could you tell me where is wrong
#include
#include
# define N 32
Struct object
{
int id;
Char * name;
Int jump;
Int ru
Int breath;
Int weight;
} class1 [N].

Void the open ();
Int main (void) {
Int number;
Printf (" 1. Query qualified students \ n ");
Printf (" 0. Exit system \ n ");
Printf (" please enter the numeric instruction: \ n ");
The scanf (" % d ", & amp; Number);

while (! (number>=0 & amp; & Number<=8))
{
Printf (" do not have this option, please enter again: \ n ");
}

The switch (number)


{case 0:
{
Printf (" a management system has become a ");
return 0;
}
Case 1:
{

Int j;
The open ();
For (j=0; j<30; J++)
{
If (class1 [j] jump> 60 & amp; & Class1 [j] run> 60)
{
Printf (" % d % d % d % d % s \ n ", class1 [j]. Journal of id, class1 [j]. Name, class1 [j]. Jump, class1 [j]. Run, class1 [j]. Journal of breath).
}
}
Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n ");


break;
}

Case 0: {
Printf (" a management system has become a ");
return 0;
break;
}

}
return 0;
}
Void the open () {
The FILE * fp.
Int I, j, nr=0;
Char buf [N], c;
Fp=fopen (" class. TXT ", "r");
If (fp==NULL) {
Printf (" file can't open the \ n ");

}
while (! The feof (fp)) {
C=fgetc (fp);
If (c=='\ n')
Nr++; }
Printf (" nr=% d \ n ", nr);
The rewind (fp);
Struct object *, line1=NULL;
, line1=(struct object *) malloc (sizeof (struct object) * (nr + 1));
For (I=1; i <=nr + 1; I++) {
The fgets (buf, N, fp);
Sscanf (buf, "% d % d % d % s \ n \ n", & amp; , line1 [I]. Id, & amp; , line1 [I]. Name, & amp; , line1 [I]. Jump, & amp; , line1 [I]. Run);
}
}


Option 1 read the file data structure is not only the result of the processing

CodePudding user response:

How do you want to deal with?

CodePudding user response:

How do you want to deal with?

CodePudding user response:

Don't see the statement

CodePudding user response:

Is in the case1

CodePudding user response:

For (j=0; j<30; J++)
{
If (class1 [j] jump> 60 & amp; & Class1 [j] run> 60)
{
Printf (" % d % d % d % d % s \ n ", class1 [j]. Journal of id, class1 [j]. Name, class1 [j]. Jump, class1 [j]. Run, class1 [j]. Journal of breath).
}

CodePudding user response:

 # include 
#include
#include

# define N 32

Struct object
{
int id;
Char * name;
Int jump;
Int the run;
Int breath;
Int weight;
} class1 [N].

Void the open ();

Int main (void)
{
Int number;
Printf (" 1. Query qualified students \ n ");
Printf (" 0. Exit system \ n ");
Printf (" please enter the numeric instruction: \ n ");
The scanf (" % d ", & amp; Number);

while (! (number>=0 & amp; & Number<=8))
{
Printf (" do not have this option, please enter again: \ n ");
}

The switch (number)
{
Case 0:
{
Printf (" a management system has become a ");
return 0;
}
Case 1:
{

Int j;
The open ();
For (j=0; j<30; J++)
{
If (class1 [j] jump> 60 & amp; & Class1 [j] run> 60)
{
Printf (" % d % d % d % d % s \ n ", class1 [j]. Journal of id, class1 [j]. Name, class1 [j]. Jump, class1 [j]. Run, class1 [j]. Journal of breath).
}
}
Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n ");

break;
}
/*
Case: 0 {//this is do what? A switch in the two case 0?
Printf (" a management system has become a ");
return 0;
break;
}
*/

}
return 0;
}
Void the open ()
{
The FILE * fp.
Int I, j, nr=0;
Char buf [N], c;
Fp=fopen (" class. TXT ", "r");
If (fp==NULL) {//since the file cannot be opened, why not quit function? Will continue to perform? Don't make a mistake like that!
Printf (" file can't open the \ n ");
return;//out

}
while (! The feof (fp)) {
C=fgetc (fp);
If (c=='\ n')
Nr++;
}
Printf (" nr=% d \ n ", nr);
The rewind (fp);
Struct object *, line1=NULL;
//, line1=(struct object *) malloc (sizeof (struct object) * (nr + 1));
, line1=(struct object *) malloc (sizeof (struct object) * (nr));
//for (I=1; i <=nr + 1; I++) {
for (i=0; i The fgets (buf, N, fp);
//sscanf (buf, "% d % d % d % s \ n \ n", & amp; , line1 [I]. Id, & amp; , line1 [I]. Name, & amp; , line1 [I]. Jump, & amp; , line1 [I]. Run);
Sscanf (buf, "% d % d % d % s \ n \ n", & amp; , line1 [I]. Id,, line1 [I]. Name, & amp; , line1 [I]. Jump, & amp; , line1 [I]. Run);
}
If (nr & gt; N)
Nr=N;
Memcpy (class1, line1, sizeof (struct object) * nr);
Free (, line1);
}


For your reference ~

First need to be processed to class1 assignment, you are to read the data into the, line1, then? The data will be automatically in class1?

CodePudding user response:

 the fgets (buf, N, fp); 

It is also likely to read the whole line of content, suggest to define buf as 128 or 256, of course, according to a row the number of bytes to ~

CodePudding user response:

For (j=0; j<30; J++)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related