| |

VerySource

 Forgot password?
 Register
Search
View: 868|Reply: 5

There was a problem writing the file ~~~

[Copy link]

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-2-11 08:00:01
| Show all posts |Read mode
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);
}
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-4-6 15:15:01
| Show all posts
while ((fwd = fwd-> next)! = NULL) // If fwd == NULL for the first time, why not leave them apart, and find yourself in trouble
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-4-6 17:45:01
| Show all posts
if (fwrite (&fwd, LEN, 1, fp)! = 1)
----------------------------
if (fwrite (fwd, LEN, 1, fp)! = 1)? ?
Reply

Use magic Report

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-4-7 23:45:01
| Show all posts
Uh. . . The above is a copy of the example question, how should I write it, I want to write the contents of each node of the linked list line by line in the file ~
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-4-8 15:45:01
| Show all posts
while (fwd! = NULL) {
   fwrite (fwd, LEN, 1, fp);
   fwd = fwd-> next;
}
// This should not be line by line, it should be piece by piece
Reply

Use magic Report

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-4-10 22:00:01
| Show all posts
How should I change it, especially the one written in the file, depressed ~~~
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list