Solved

sscanf convert C-strings and floating point in one line

Posted on 2010-09-06
11
666 Views
Last Modified: 2012-05-10
I have the follow C code to read a line from a file "store-data.txt" into a C string "record"
and then I use the sscanf to convert into two strings and a floating point with the format string:
 char format1[] = "%s %s %6.2f" and the file contains

"Royal_Crown  CrownRoyal  35.99"

The problem is it doen't read the floating point correctly while after taking the two strings.
The output was:

Got the record from the file: Royal_Crown  CrownRoyal 35.99
Royal_Crown  CrownRoyal  1.22298e-38

Thanks
------------------ The code:

char field_winename[20];
        char field_producer[20];
        float field_price;

 //
      ifstream infile("store-data.txt");
        infile.getline(record,80);   // read in a line from store-data.txt, into  "record"
        cout << "Got the record from the file: " << record << endl;

        num_words = sscanf(record,format1,field_winename,
                                          field_producer,
                                          &field_price
                          );
0
Comment
Question by:zzhang2006
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 33612502
Why not read the line into a std::string rather than a char array? Once it's in the string use the string to initialise an istringstream and then read the specific values of from that.

http://www.cplusplus.com/reference/iostream/istringstream/
http://www.cplusplus.com/reference/string/getline/
http://www.cplusplus.com/reference/string/string/


//Untested example
string line;

ifstream infile("store-data.txt");
getline(infline, line);
istringstream ss(line);

string field_winename;
string field_producer;
float field_price;

ss >> field_winename;
ss >> field_producer;
ss >> field_price;

Open in new window

0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
ID: 33612504
try with this:
char format1[] = "%[^ ]s %[^ ]s %6.2f"
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33612516
BTW are you away you only put this in the C zone but you are using C++ constructs? Would you like that zone added?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 33612551
the question is very clear: sscanf convert C-strings and floating point in one line
So, it is a C-Language question, although other portions of the code are C++ related.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33612567
>> although other portions of the code are C++ related.
Meaning it *is* C++ code meaning we *can* look at exploring C++ solutions. Thanks for telling a Zone Advisor how to do his job.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 150 total points
ID: 33612574
Just use %f instead of %6.2f. The latter is not valid for sscanf.
0
 
LVL 1

Author Closing Comment

by:zzhang2006
ID: 33612676
Thank you all!
Intinity08's solution worked and I will try Jaime's too.

That's all I need for now
0
 
LVL 1

Author Comment

by:zzhang2006
ID: 33612680
thank again
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33612719
jaime_olivares' post would help if the first two tokens (the strings) can contain spaces. It would not solve the problem with the floating point value though. ie. it's solving a different problem than the one you're having.

Do look into what evilrix suggested though. Using std::string's is easier and safer in most cases.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33612771
>> jaime_olivares' post would help if the first two tokens (the strings) can contain spaces.

Correction : that's not true. I misread what jaime_olivares suggested. Using %[^ ] allows the first two tokens to contain tabs, not spaces ;) On the condition that you use %[^ ] and not %[^ ]s ...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 33613081
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to show menu 5 107
Arduino EDI - Programming language 3 128
Computer slow / BSOD 10 79
How to pass pointer to int when expected type is pointer to const char *   ? 6 76
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question