View Full Version : How to convert QString to char *
rajeshs
27th September 2007, 08:28
I want to use sscanf function ,
I am using this function as follows,
QString s=" " //Some data;
sscanf(s,format,value1,value2 ,...);
this shows error like can't convert QString to char *,
So i tried ,
(char*)(s.data()) this is returning only first characcter,
usage like this,
sscanf((char*)(s.data()),format,value1,value2 ,...);
How to convert QString to char *?
Please help me.
Thanks,
Rajesh.S
jpn
27th September 2007, 09:06
I'd rather use for example QTextStream instead of sscanf. But if you insist, How can I convert a QString to char* and vice versa ? (http://trolltech.com/developer/knowledgebase/faq.2007-01-30.9032238253/)
PS. QString to char* conversion has been discussed millions of times..
rajeshs
27th September 2007, 09:36
Sorry , I searched for that i didn't get, Please post that links,
Thanks,
Rajesh.S
jpn
27th September 2007, 09:39
What's wrong with the link to Trolltech Knowledgebase?
rajeshs
27th September 2007, 09:42
sory in that link i didn't get clear idea.
Thanks
Rajesh.S
rajeshs
27th September 2007, 09:45
Sorry I went wrong link,.
Now i got the correct code
QApplication app(argc, argv);
QString str1 = "Test";
QByteArray ba = str1.toLatin1();
const char *c_str2 = ba.data();
printf("str2: %s", c_str2);
return app.exec();
Thank you for your valuable reply.
Thanks,
Rajesh.S
mtrpoland
27th September 2007, 09:49
And what about:
qString.toStdString().c_str();
?
wysota
27th September 2007, 10:32
And what about:
qString.toStdString().c_str();
?
You're asking yourself for a segfault here if you intend to ever use the resulting char*. The std::string is a temporary object that will get destroyed immediately, so such a construction is useless. If you look at the example, it avoids creating a temporary variable. But if you avoid it, you can of course go through std::string, if you have STL support compiled into Qt.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.