Home > Back-end >  For help, the C language to read data from a file to list and will list save to file
For help, the C language to read data from a file to list and will list save to file

Time:09-27

Leaders should take a look at this how change,

//from a specific file read information 
Createlink struct Node * (void)
{
Struct Node * phead=(struct Node *) malloc (sizeof (struct Node));
Struct Node * ptail;
Struct Node * pnew;
Ptail=pnew=phead;
The FILE * fp=fopen (" book. TXT ", "r");
If (fp==NULL)
{
Printf (" failed to open file!" );
return NULL;
}
while(! The feof (fp))
{
Pnew=(struct Node *) malloc (sizeof (struct Node));
Fread (& amp; Pnew - & gt; N, pnew - & gt; N, sizeof (pnew - & gt; N), fp);
The fgets (pnew - & gt; The name, sizeof (pnew - & gt; Name), fp);
The fgets (pnew - & gt; Place, sizeof (pnew - & gt; Place), fp);
The fgets (pnew - & gt; The writer, sizeof (pnew - & gt; Writer), fp);
Fread (& amp; Pnew - & gt; Price, pnew - & gt; Price, sizeof (pnew - & gt; Price), fp);
Ptail - & gt; Next=pnew;
Pnew - & gt; Next=NULL;
Ptail=pnew;
}
fclose(fp);
Return phead;
}


//to write information into the specific file
Void WriteToFile (struct Node * phead)
{
Struct Node * write;
Write=phead - & gt; next;
FILE *fp;
If ((fp=fopen (" book. TXT ", "w"))==NULL)
{
Printf (" open failure! \ n \ n ");
exit(0);
}
while(NULL!=write)
{
Fwrite (& amp; The write - & gt; N, the write - & gt; N, sizeof (write - & gt; N), fp);
The fputs (write - & gt; The name, fp);
The fputs (write - & gt; Place, fp);
The fputs (write - & gt; Writer, fp);
Fwrite (& amp; The write - & gt; Price, the write - & gt; Price, sizeof (write - & gt; Price), fp);
Write=write - & gt; next;
}
If (fclose (fp))
{
Printf (" can't shut down! \n");
exit(0);
}
}

CodePudding user response:

Fwrite (& amp; The write - & gt; N, the write - & gt; N, sizeof (write - & gt; N), fp);
The fputs (write - & gt; The name, fp);
The fputs (write - & gt; Place, fp);
The fputs (write - & gt; Writer, fp);
Fwrite (& amp; The write - & gt; Price, the write - & gt; Price, sizeof (write - & gt; Price), fp)

Fwrite (write, sizeof (struct Node), 1, fp);

CodePudding user response:

reference 1st floor CHXCHXKKK response:
fwrite (& amp; The write - & gt; N, the write - & gt; N, sizeof (write - & gt; N), fp);
The fputs (write - & gt; The name, fp);
The fputs (write - & gt; Place, fp);
The fputs (write - & gt; Writer, fp);
Fwrite (& amp; The write - & gt; Price, the write - & gt; Price, sizeof (write - & gt; Price), fp)

Fwrite (write, sizeof (struct Node), 1, fp);

This I see don't quite understand,,, bother details

CodePudding user response:

The whole structure is as a piece of writing, rather than a a written inside
  • Related