I'm trying to scanf sql table without using scanf for every symbol, but my code outputs some kind of trash. Input
8
"Peter Falk" 1927 "USA"
"Oleg Tabakov" 1935 "USSR"
"Andrei Mironov" 1941 "USSR"
"Arnold Schwarzenegger" 1947 "USA"
"Jean Reno" 1948 "France"
"Sharon Stone" 1958 "USA"
"Tom Cruise" 1962 "USA"
"Ryoko Hirosue" 1980 "Japan"
My code
#include <stdio.h>
#include <stdlib.h>
typedef struct ActorBio {
char name[35];
int BirthYear;
char country[15];
} ActorBio;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n1;
scanf("%d", &n1);
ActorBio *actors = malloc(sizeof(ActorBio) * (n1 + 2));
getchar();
for (int i = 0; i < n1; i++) {
scanf("%[^34]s\n", actors[i].name);
scanf("%d\n",&actors[i].BirthYear);
gets(actors[i].country);
printf("%s %d %s\n",actors[i].name,actors[i].BirthYear,actors[i].country);
}
return 0;
}
I use scanf("%[^34]s\n", actors[i].name); to scanf until 34(asci code of "), but it just scanfs everything.Then i scanf int data and then country. How should i change this part to scanf only name? My output is
"Peter Falk" 1927 "USA"
"Oleg Tabako# 35 "USSR"
"Andrei Mironov" 19 41 "USSR"
"Arnold Schwarzenegger" 19 47 "USA"
"Jean Reno" 19 48 "France"
"Sharon Stone" 1958 "USA"
"Tom Cruise" 1962 "USA"
"Ryoko Hirosue" 1980 "Japan" 824189541 962 "USA"
"Ryoko Hirosue" 1980 "Japan"
Hirosue" 1980 "Japan" 0
0
0
I want my struct ActorBio to save information correctly and to use scanfs instead of while loops.