Home > Back-end >  Correct mistakes
Correct mistakes

Time:05-24

/*
C
@ Filename: ex804.@ the Author: Ju Chengdong
@ Version: 1.0
@ Date: 2021-03-18
@ Description: Format Input and the Output
*/

# include & lt; Stdio. H>

Struct stu {
Int id;//student id
Char name [20].//name
Int the age;//age
Int score;//achievement
};

Int main () {
/* declare functions and variables */
Void readFileForStu (struct stu * p, int num, char filename [], char mode []);
Void stdoutForStu (struct stu * p, int num);
Const int NUM=2;//the number of students
Struct stu students (NUM);/* u array
Struct stu * p=students;/* u pointer
Char filename []="ex804. TXT".//to read the file path and name

Get input from the file/* */
ReadFileForStu (p, NUM, filename, "r");

/* output to the screen display */
StdoutForStu (p, NUM);
return 0;
}

/*
* function name: readFileForStu
* functions: data read from the file format
* written format: reading num line; Each read a stu type data, separated by a space between each member variable
* form parameters: struct stu * p, pointing to stu first address a one-dimensional array, the array is used to save the file to read data
* form parameters: int num, one-dimensional array element number, i.e. the number of rows read file
* form parameters: char filename [], to read the file path and name
* form parameters: char mode [], the file use
No
* to return back to value:*/
Void readFileForStu (struct stu * p, int num, char filename [], char mode []) {
//please programming to realize the function
int i;
for(i=0; i{
The scanf (" % d % d % d % s ", & amp; P [I]. Id, p [I]. Name, & amp; P [I]. Age, & amp; P [I] score);
}
FILE *fp;
Fp=fopen (" ex804. TXT ", "mode");
for(i=0; i{
Fwrite (& amp; P [I], sizeof (struct stu), 1, fp);
printf("\n");
}
fclose(fp);


}

/*
* function name: stdoutForStu
Features: * function to display the output display data
* the output format: total output num line; Each row of the output a stu type data, separated by a space between each member variable
* form parameters: struct stu * p, pointing to stu type a one-dimensional array first address
* form parameters: int num, one-dimensional array element number
No
* to return back to value:*/
Void stdoutForStu (struct stu * p, int num) {
//please programming to realize the function
int i;
FILE *fp;
Fp=fopen (" ex804. TXT ", "rb");
for(i=0; i{
Fread (& amp; P [I], sizeof (struct stu), 1, fp);
Printf (" % d % d % d % s \ n ", p [I] id, p [I]. Name, p [I] age, p [I] score).
}
fclose(fp);
}

Where is the code is wrong?

CodePudding user response:

Fp=fopen (" ex804. TXT ", "mode"); This statement does not, it should be fp=fopen (" ex804. TXT ", mode).
This statement mistake can lead to file cannot be opened, and cause the fp is NULL, behind may lead to abnormal,

CodePudding user response:

Modified as follows, for your reference:
 # include & lt; Stdio. H> 

Struct stu {
Int id;//student id
char name[20];//name
Int the age;//age
Int score;//achievement
};

Int main () {
/* declare functions and variables */
Void readFileForStu (struct stu * p, int num, char filename [], char mode []);
Void stdoutForStu (struct stu * p, int num);
Const int NUM=2;//the number of students
Struct stu students (NUM);//stu type array
Struct stu * p=students;//pointer to stu type one dimensional array type
Char filename []="ex804. TXT".//to read the file path and name

Get input from the file/* */
ReadFileForStu (p, NUM, filename, "r");

/* output to the screen display */
StdoutForStu (p, NUM);

return 0;
}

/*
* function name: readFileForStu
* functions: data read from the file format
* written format: reading num line; Each read a stu type data, separated by a space between each member variable
* form parameters: struct stu * p, pointing to stu first address a one-dimensional array, the array is used to save the file to read data
* form parameters: int num, one-dimensional array element number, i.e. the number of rows read file
* form parameters: char filename [], to read the file path and name
* form parameters: char mode [], the file use
No
* to return back to value:*/
Void readFileForStu (struct stu * p, int num, char filename [], char mode [])
{
//please programming to realize the function
int i;
FILE *fp;
Fp=fopen (filename, mode);
If (fp==NULL) {
Printf (" Open file fail! \n");
return;
}
for(i=0; i{
Fread (& amp; P [I], sizeof (struct stu), 1, fp);
}
fclose(fp);
}

/*
* function name: stdoutForStu
Features: * function to display the output display data
* the output format: total output num line; Each row of the output a stu type data, separated by a space between each member variable
* form parameters: struct stu * p, pointing to stu type a one-dimensional array first address
* form parameters: int num, one-dimensional array element number
No
* to return back to value:*/
Void stdoutForStu (struct stu * p, int num)
{
//please programming to realize the function
int i;
for(i=0; iPrintf (" % d % d % d % s \ n ", p [I] id, p [I]. Name, p [I] age, p [I] score).
}

//1 JJJJ 18 90
//2 KKKK 19 91
//please press any key to continue...

CodePudding user response:

refer to the second floor QZJHJXJ response:
modify as follows, for reference:
 # include & lt; Stdio. H> 

Struct stu {
Int id;//student id
char name[20];//name
Int the age;//age
Int score;//achievement
};

Int main () {
/* declare functions and variables */
Void readFileForStu (struct stu * p, int num, char filename [], char mode []);
Void stdoutForStu (struct stu * p, int num);
Const int NUM=2;//the number of students
Struct stu students (NUM);//stu type array
Struct stu * p=students;//pointer to stu type one dimensional array type
Char filename []="ex804. TXT".//to read the file path and name

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related