CodeFu
CodeFu

Reputation: 165

sscanf input not working

I have tab seperated records like this

1000    Muhammad Aashir 0213-4211685    123456  0

first I have read the line by using fgets and now i am trying to extract contents by using sscanf, but there is an unexpected problem... please help I am beginner

here is the code

char buffer[SIZE];
Account req;
while(fgets(buffer,SIZE,fptr))
{
    cout<<endl<<buffer<<endl;
    sscanf(buffer,"%d\t%s\t%s\t%s\t%ld\n",&req.acc_num,req.name,req.mobileno,req.pass,&req.acc_bal);
    cout<<endl<<req.pass;
}

output of BUFFER is same as the record line

but after extracting values, when I am displaying the 'req.pass' the value is incorrect

req.pass is displaying '0213-4211685' but it has to display '123456'

Upvotes: 1

Views: 80

Answers (1)

Corb3nik
Corb3nik

Reputation: 1177

sscanf will capture until reaching any kind of whitespace. In your case, req.name only contains Muhammad. This will cause the rest of your variables to contain the wrong info.

If you need to use sscanf(), you'll have to replace instances of " " in your name with an escape character, like "_" for example.

Upvotes: 3

Related Questions