我想创建一个函数来读取用C动态分配的字符串,但是程序会读取struct的两个字段。 T_magazzino nuova struttura创建动态分配和char * s_gets读取字符串
char * s_gets(char * st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n'); // look for newline
if (find) // if the address is not NULL,
*find = '\0'; // place a null character there
else
while (getchar() != '\n')
continue; // dispose of rest of line
}
return ret_val;
}
t_magazzino *nuova_struttura(void)
{
t_magazzino *a;
a=(t_magazzino*)malloc(sizeof(t_magazzino));
if(a==NULL)
{
puts("Errore");
exit(1);
}
void item(t_magazzino *a, FILE *f,int c)
{
puts("Inserisci ID");
scanf("%d",&a->id);
puts("Inserisci cliente \n");
scanf("%s",a->client);
printf(" Inserire tipologia \n");
scanf("%s",a->tiplogia);
}