|
You need to write a set of student data, including student number, name, grades of three courses, and average points. The errors are as follows:
while (fwd! = NULL)
{
if (fwrite (&fwd, LEN, 1, fp)! = 1) // write the information of each row in turn
printf ("Finish writing\n");
fwd = fwd-> next;
}
There are sub-functions that are isolated by comments. They want to read the data from the file and output it. See if there is anything wrong. . .
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define NULL 0
#define LEN sizeof (struct stu_data)
#define F_NAMELEN 10
#define STU_NAMELEN 10
#define STU_MAX 5
#define CRS_MAX 3
struct stu_data
{
long num;
char name [STU_NAMELEN];
float course [CRS_MAX];
float aver;
struct stu_data * next;
};
FILE * fp;
struct stu_data * Crt (void) /// Create a linked list and write student data
{
int course;
struct stu_data * head, * fwd, * bk;
head = bk = (struct stu_data *) malloc (LEN); // The first node does not store information
fwd = head-> next = (struct stu_data *) malloc (LEN); // Open a second node
printf ("Max student% d Max course% d\n", STU_MAX, CRS_MAX);
printf ("Now input the infomations please\n");
printf ("Input 0 as number to cease\n"); // Output three lines of prompt information
while (1)
{
printf ("Num =>");
scanf ("% ld",&fwd-> num); // Enter the student number
if (! fwd-> num)
break;
printf ("Name =>");
scanf ("% s",&fwd-> name); // Enter the name
for (fwd-> aver = 0, course = 0; course <CRS_MAX; course ++)
{
printf ("Subject% d =>", course + 1);
scanf ("% f",&fwd-> course [course]); // Enter the results
fwd-> aver + = fwd-> course [course];
}
fwd-> aver / = CRS_MAX; // Calculate the average score
bk = fwd;
fwd = fwd-> next = (struct stu_data *) malloc (LEN); // Open up the next space
}
bk-> next = NULL;
return head;
}
void save_file (struct stu_data * fwd) // Save the linked list in the newly created file
{
if ((fp = fopen ("stu_data.txt", "wb")) == NULL)
{
printf ("File open failed\n");
exit (0);
/ * return; * /
}
while (fwd! = NULL)
{
if (fwrite (&fwd, LEN, 1, fp)! = 1)
printf ("Finish writing\n");
fwd = fwd-> next;
}
}
/ * void print (struct stu_data * fwd) // Open the file and output the stored information
{
rewind (fp);
printf ("Now look into the document contents\n");
while ((fwd = fwd-> next)! = NULL)
{
fwrite (fwd, LEN, 1, fp);
printf ("\n");
}
fclose (fp);
} * /
void main ()
{
struct stu_data * head;
head = Crt ();
save_file (head);
// print (head);
} |
|