Home > Back-end >  Try to C language program reads a TXT file and the group of data input structure, but has been "
Try to C language program reads a TXT file and the group of data input structure, but has been "

Time:09-20

TXT file is

I's structure is like this
 typedef struct {
int num;
char name[20];
int age;
Float score;
} datatype.
Typedef struct {
Datatype data (MAX);
Int listlen;
} sqlist;

Read the file FileRead () function of the
 void FileRead (sqlist * L) 
{
The FILE * fp.
Char filename [40].
Printf (" please enter the file name (at the end with a space as the string terminator) : \ n ");
Scanf_s (" % s ", & amp; The filename, 40);
Printf (" how many sets of data in the file?" );
Scanf_s (" % d ", & amp; L - & gt; Listlen);
If ((fp=fopen (filename, "r"))==NULL)
return;
for (int i=0; I & lt; L - & gt; Listlen; I++) {
Fscanf_s (fp, "% d % d % s & amp; F ", & amp; L - & gt; Data [I]. Num, L - & gt; Data [I]. Name, & amp; L - & gt; Data [I]. Age, & amp; L - & gt; Data [I] score);
}
Printf (" \ n data import success!" );
_getch ();
}

Read files in the main function of the part is
 int main () 
{
Sqlist * L=(sqlist *) malloc (sizeof (sqlist));
FileRead (L);

Used to output the OUT function is
 void OUT (sqlist * L) 
{
for (int i=0; I & lt; L - & gt; Listlen; I++) {
Printf (" % 6 6 d % s % d % 6.1 f \ n ", L - & gt; Data [I]. Num, L - & gt; Data [I]. Name, L - & gt; Data [I]. Age, L - & gt; Data [I] score);
}
}

A read operation output information it appears full screen tuen tuen tuen, could you tell me how to solve? I very vegetables, just learning data structure

CodePudding user response:

Dynamic allocation (new, malloc) without initialization of memory space, with 0 XCD filling, such as accidentally visited them, you'll see the following content:
According to the character to the output the chariot (0 XCDCD)

You failed to read file scanf_s (" % s ", & amp; The filename, 40); If no & amp; Scanf_s (" % s ", filename, 40).
  • Related