Home > Enterprise >  Scanf table C using scanf
Scanf table C using scanf

Time:12-17

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.

CodePudding user response:

int n1;
scanf("%d", &n1);

There is still one \n character in the buffer, this will be read the next time. You can skip it with scanf("%d\n", &n1)

You also want to make sure the function succeeded, otherwise n1 is not initialized. You only need to allocate n1 items.

int n1;
if (scanf("%d\n", &n1) != 1) 
{ printf("format error\n"); return 0; }

ActorBio* actors = malloc(sizeof(ActorBio)* n1);
if (!actors) { return 0; }

for (int i = 0; i < n1; i  )
{
    if (3 != scanf("\"4[^\"]\" %d \"[^\"]\"\n", 
        actors[i].name, &actors[i].BirthYear, actors[i].country))
        continue;
    printf("%s %d %s\n", actors[i].name, actors[i].BirthYear, actors[i].country);
}

Alternatively you can use fgets and strtok

char buf[1024];
for (int i = 0; i < n1;)
{
    if (!fgets(buf, sizeof(buf), stdin))
        break;
    
    memset(&actors[i], 0, sizeof(ActorBio));

    char* token;
    token = strtok(buf, "\"");
    if (!token) continue;
    strncpy(actors[i].name, token, sizeof(actors[i].name) - 1);

    token = strtok(NULL, " ");
    if (!token) continue;
    actors[i].BirthYear = atoi(token);

    token = strtok(NULL, "\"");
    if (!token) continue;
    strncpy(actors[i].country, token, sizeof(actors[i].country) - 1);

    printf("%s %d %s\n", actors[i].name, actors[i].BirthYear, actors[i].country);

    i  ;
}
  • Related