[Qt-interest] Segmentation fault while converting from QString to char*

Nikos Chantziaras realnc at arcor.de
Mon Apr 19 17:53:25 CEST 2010


On 04/19/2010 10:56 AM, prashant bhutani wrote:
> Hi everyone,
>
> I am making a program which fetch a file from directory using
> *getOpenFileName* and it returns the path as a *QString*.
> Now I have to convert the path in *char** which i am doing using
> *QByteArray ba = qt_file_name.toLatin1(); *//qt_file_name is path of
> file in Qstring
> *         const char* temp = ba.data(); *
> *         strcpy(file_name,temp); *// file_name is in char*
>
> Now the code is causing segmentation fault in my program as it runs fine
> when these statements are made comments.

Try:

   QByteArray ba = qt_file_name.toLatin1();
   strncpy(file_name, ba.constData(), file_name_size);

'file_name_size' is the size of the 'file_name' target buffer (for 
example "100" if you allocated file_name with "file_name = new char[100]".)



More information about the Qt-interest-old mailing list