Home > Back-end >  O bosses guidance error, the file output gap
O bosses guidance error, the file output gap

Time:12-06

//the header file
# define _CRT_SECURE_NO_WARNINGS
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; String. H>
# include
//extern list_single * list=createList ();
Struct student
{
Char name [20].
Char sex [2];
Char num [20].
Float math;
};
Struct list_node
{
Struct student data;
Struct list_node * next;
};

Typedef struct list_node list_single;
//create a list
List_single * createList ()
{
List_single * head_Node=(list_single *) malloc (sizeof (list_single));
Head_Node - & gt; Next=NULL;
Return head_Node;
}

//create a new node
List_single * createNode (struct student data)
{
List_single * new_Node=(list_single *) malloc (sizeof (list_single));
New_Node - & gt; Data=https://bbs.csdn.net/topics/data;
New_Node - & gt; Next=NULL;
Return new_Node;
}
//traverse the list
Void print_list (list_single * head_Node)
{
List_single * p_Move=head_Node - & gt; Next;
Printf (" num \ tname \ tsex \ tmath \ n ");
While (p_Move)
{
Printf (" % s \ \ t t % s % s \ t % 2 f \ n ", p_Move - & gt; Data. The num, p_Move - & gt; Data. The name, p_Move - & gt; Data. The sex, p_Move - & gt; Data. The math);
P_Move=p_Move - & gt; Next;
}
printf("\n");
}
//insert the new node, head of interpolation
Void insert_Node_by_Head (list_single * head_Node, struct student data)
{
List_single * new_Node=createNode (data);
New_Node - & gt; Next=head_Node - & gt; Next;
Head_Node - & gt; Next=new_Node;
}
//node specified delete
Void delete_Node_by_Appion (list_single * head_Node, char * num)
{
List_single * pos_Node=head_Node - & gt; Next;
List_single * pos_Front=head_Node;
If (pos_Node==NULL)
{
Printf (" didn't find relevant information, delete failed ");
}
The else
{
While (STRCMP (pos_Node - & gt; Data. The num, num))
{
Pos_Front=pos_Node;
Pos_Node=pos_Front - & gt; Next;
If (pos_Node==NULL)
{
Printf (" relevant information was not found ");
return;
}

}
Pos_Front - & gt; Next=pos_Node - & gt; Next;
Free (pos_Node);
}
}
//to find information
List_single * search_Information (list_single * head_Node, char * num)
{
List_single * p_Move=head_Node - & gt; Next;
If (p_Move==NULL)
return NULL;
While (STRCMP (p_Move - & gt; Data. The num, num))
P_Move=p_Move - & gt; Next;
Return p_Move;
}
//read the file
Void Read_File (list_single * head_Node, char * File_Name)
{
The FILE * fp.
Struct student data;
Fp=fopen (" File_Name ", "r");
If (fp==NULL)
{
Printf (" error: failed to open the file ");
exit(0);
}
While (fscanf (fp, "% s \ \ t % s \ % f t t % s \ n", & amp; Data. The num, & amp; Data. The name, & amp; Data, sex, & amp; The data math)!=(EOF)
{
Insert_Node_by_Head (head_Node, data);//node insert list
}
fclose(fp);
}


//file write
Void Write_File (list_single * head_Node, char * File_Name)
{
The FILE * fp.
Fp=fopen (File_Name, "w");
List_single * p_Move=head_Node - & gt; Next;
While (p_Move)
{
Fprintf (fp, "% s \ \ t % s \ % f t t % s \ n", p_Move - & gt; Data. The num, p_Move - & gt; Data. The name, p_Move - & gt; Data. The sex, p_Move - & gt; Data. The math);
P_Move=p_Move - & gt; Next;
}
fclose(fp);
}
//source # include "single _list. H"
Void Menu ();
Void Uesr_Key ();
Void Add_Information ();
Void Printf_Information ();
Void Deletet_Information ();
List_single * list=createList ();
Void main ()
{
The Read_File (list, "1. TXT");//read the file
Print_list (list);//print list
/* the while (1)
{
The Menu ();
Uesr_Key ();
} */

}















File creation is no problem, content is written to the file is successful, but the output file when it's all blank, bosses give directions, small white master c language, thank you for big help

CodePudding user response:

Read and write files refer to the
I use this app # CSDN# found have technical content of the blog and friends to seek common to "C language file reading and writing (1) - the text file read operation", gathered together at https://blog.csdn.net/zhanghaiyang9999/article/details/107032563? Utm_source=app

CodePudding user response:

 
Void Read_File (list_single * head_Node, char * File_Name)
{
The FILE * fp.
Struct student data;

//error here, File_Name
with double quotation marks//fp=fopen (" File_Name ", "r");

Fp=fopen (File_Name, "r");
If (fp==NULL)
{
Printf (" error: failed to open the file ");
exit(0);
}
While (fscanf (fp, "% s \ \ t % s \ % f t t % s \ n", & amp; Data. The num, & amp; Data. The name, & amp; Data, sex, & amp; The data math)!=(EOF)
{
Insert_Node_by_Head (head_Node, data);//node insert list
}
fclose(fp);
}
  • Related