Home > Back-end >  C language exam correction
C language exam correction

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;
If ((fp=fopen (" ex804. TXT ", mode))==NULL)
{
Printf (" always open this file \ n ");
return;
}
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;
If ((fp=fopen (" ex804. TXT ", "rb"))==NULL)
{
Printf (" always open this file \ n ");
return;
}
For (I=0; i{
Fread (& amp; P [I], sizeof (struct stu), 1, fp);
Printf (" % 4-10 s % 4 d % d % 4 d \ n ", p [I] id, p [I]. Name, p [I] age, p [I] score).
}
fclose(fp);
}


What's wrong with the excuse me, is leading to output the result?

CodePudding user response:

It is done homework help

CodePudding user response:

Single-step tracking, can know where is wrong, if you want to learn a programming language, with good development environment of the debug function is at least the skills,

CodePudding user response:

To change this, it is ok to should be:
 # 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

///file access to enter
ReadFileForStu (p, NUM, filename, "r");

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

return 0;
}

//
//* function name: readFileForStu
//* function functions: data read from the file format
//* written formats: read num line; Each read a stu type data, separated by a space between each member variable
//* form parameters: struct stu * p, pointing to the 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, which is the number of rows read file
//* form parameters: char filename [], to read the file path and name of the
//* form parameters: char mode [], the file use
//* to return back to value: no

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);
Fscanf (fp, "% d % d % d % s", & amp; P [I]. Id, p [I]. Name, & amp; P [I]. Age, & amp; P [I] score);
}
fclose(fp);
}

//
//* function name: stdoutForStu
Function://* function to display the output display data
//* 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 the stu type a one-dimensional array first address
//* form parameters: int num, one-dimensional array element number
//* to return back to value: no

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 100 100 libai
//2 lihei 200 200
//please press any key to continue...

//10001 ZhangSan 25 99
//10002 ZhaoLiu 18 100
//please press any key to continue...
  • Related