Home > Back-end >  , for help
, for help

Time:01-23

A homework assignments after class:
1. The following structure type variable, in the form of binary storage to a file, and can achieve read interface, print read information,
Typedef struct Student
{
Char * name;//name
int id;
Int name_len;//the name length
}Stu;

Stu s;//structure variables
//structure members assignment
S.i d=1;
S.n ame_len=strlen (" mike ");//the name length
S.n ame=(char *) malloc (s.n ame_len + 1);
Strcpy (s.n ame, "mike");


Then my code is as follows:
# define _CRT_SECURE_NO_WARNINGS
# include
# include
# include

Typedef struct Student
{
Char * name;//name
int id;
Int name_len;//the name length
}Stu;

Int main0101 ()
{
//write
Stu s;
S.i d=1;
S.n ame_len=strlen (" mike ");//the name length
S.n ame=(char *) malloc (s.n ame_len + 1);
Strcpy (s.n ame, "mike");
The FILE * fp=fopen (" selfSu. TXT ", "wb");
if (! Fp)
return -1;

Fwrite (& amp; S, sizeof (s), 1, fp);
The fclose (fp);

return 0;
}

Int main ()
{
//read
The FILE * fp=fopen (" selfSu. TXT ", "rb");
if (! Fp)
return -1;
Stu s;

Fread (& amp; S, sizeof (Stu), 1, fp);

The fclose (fp);

Name: printf (" % s \ t number: % d \ t name length: % d \ n ", s.n ame, s.i d, s.n ame_len);

return 0;
}

Why has been an error? Error is as follows:

CodePudding user response:

 # define _CRT_SECURE_NO_WARNINGS 
# include
# include
# include

Typedef struct Student
{
Char * name;//name
int id;
Int name_len;//the name length
}Stu;

Int main0101 ()
{
//write
Stu s;
S.i d=1;
S.n ame_len=strlen (" mike ");//the name length
S.n ame=(char *) malloc (s.n ame_len + 1);
Strcpy (s.n ame, "mike");
The FILE * fp=fopen (" selfSu. TXT ", "wb");
if (! Fp)
return -1;

Fwrite (& amp; S, sizeof (s), 1, fp);
The fclose (fp);

return 0;
}

Int main ()
{
Main0101 ();//write can read
//read
The FILE * fp=fopen (" selfSu. TXT ", "rb");
if (! Fp)
return -1;
Stu s;

Fread (& amp; S, sizeof (Stu), 1, fp);

The fclose (fp);

Name: printf (" % s \ t number: % d \ t name length: % d \ n ", s.n ame, s.i d, s.n ame_len);

Free (s.n ame);

return 0;
}

For your reference ~

CodePudding user response:

The
reference 1/f, confident boy reply:
 # define _CRT_SECURE_NO_WARNINGS 
# include
# include
# include

Typedef struct Student
{
Char * name;//name
int id;
Int name_len;//the name length
}Stu;

Int main0101 ()
{
//write
Stu s;
S.i d=1;
S.n ame_len=strlen (" mike ");//the name length
S.n ame=(char *) malloc (s.n ame_len + 1);
Strcpy (s.n ame, "mike");
The FILE * fp=fopen (" selfSu. TXT ", "wb");
if (! Fp)
return -1;

Fwrite (& amp; S, sizeof (s), 1, fp);
The fclose (fp);

return 0;
}

Int main ()
{
Main0101 ();//write can read
//read
The FILE * fp=fopen (" selfSu. TXT ", "rb");
if (! Fp)
return -1;
Stu s;

Fread (& amp; S, sizeof (Stu), 1, fp);

The fclose (fp);

Name: printf (" % s \ t number: % d \ t name length: % d \ n ", s.n ame, s.i d, s.n ame_len);

Free (s.n ame);

return 0;
}

For reference ~

The building Lord so defined structure and read easily lead to memory overflow ~ released in the main function,

CodePudding user response:

You obviously lack the malloc, so name have no place to put,

CodePudding user response:

Oh, name didn't write into,
Subject need you write another name, can't write light structure,

CodePudding user response:

The
reference 1/f, confident boy reply:
 # define _CRT_SECURE_NO_WARNINGS 
# include
# include
# include

Typedef struct Student
{
Char * name;//name
int id;
Int name_len;//the name length
}Stu;

Int main0101 ()
{
//write
Stu s;
S.i d=1;
S.n ame_len=strlen (" mike ");//the name length
S.n ame=(char *) malloc (s.n ame_len + 1);
Strcpy (s.n ame, "mike");
The FILE * fp=fopen (" selfSu. TXT ", "wb");
if (! Fp)
return -1;

Fwrite (& amp; S, sizeof (s), 1, fp);
The fclose (fp);

return 0;
}

Int main ()
{
Main0101 ();//write can read
//read
The FILE * fp=fopen (" selfSu. TXT ", "rb");
if (! Fp)
return -1;
Stu s;

Fread (& amp; S, sizeof (Stu), 1, fp);

The fclose (fp);

Name: printf (" % s \ t number: % d \ t name length: % d \ n ", s.n ame, s.i d, s.n ame_len);

Free (s.n ame);

return 0;
}

For reference ~


The
reference 1/f, confident boy reply:
 # define _CRT_SECURE_NO_WARNINGS 
# include
# include
# include

Typedef struct Student
{
Char * name; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related