sscanf is failing to get string from file.


string data;
string stamp;
string temp = "deepesh 12 2009-10-10_00:31:12";
sscanf( temp.c_str(), "%s%d%s", data.c_str(), &value, stamp.c_str() );
cout<<data.c_str()<<endl;
cout<<stamp.c_str()<<endl;

if i try to print data and stamp."2009-10-10_00:31:12" is printing for both.

only last string is printing every time.
If i tried with character array it is working fine.

Please help me to solve this.
you can't use the C-function sscanf to fill the c++ string "data". Use a char-array or vector (you have to make assumptions about the length of the prefix-name and date).

But better use istringstream for it. Much less hassle.

1
2
3
4
5
6
string data;
string stamp;
string temp = "deepesh 12 2009-10-10_00:31:12";
istringstream(temp) >> data >> value >> stamp;
cout<<data<<endl; // no need for .c_str() here
cout<<stamp<<endl; 


Ciao, Imi
Last edited on
Thanks.its worked.
Topic archived. No new replies allowed.