PDA

View Full Version : Problem in conversion of QChar to char



Prashant_Bhutani
19th April 2010, 01:40
Hi Everyone,

I know this topic has been discussed a lot in last decade but believe me I am posting my problem after reading all that stuff.

Motive of my program:
Fetch a file (basically image file) from the directory which I am doing using getOpenNewFile, which returns the name of file in QString format.
I want to convert that QString name in char* format.

Now the Problem
I have applied all techniques:
1) str[i] = static_cast<char>(Qstr.at(i).toAscii())
2) str[i] = static_cast<char>(Qstr.at(i).toLatin1())
and similarly for whole string.
but no success. My program is taking the file name successfully but returns segmentation fault just after that.

Please do tell me if there's any other method for doing this.
Thanks in advance.

Regards,
Prashant

Lesiok
19th April 2010, 07:53
Please show us real code.

Prashant_Bhutani
19th April 2010, 08:11
// fetching the file from directory
QString file = QFileDialog::getOpenFileName(this,tr("open file"),QDir::currentPath());
// showing file path on QLineEdit
file_path->setText(file);
// the file path is in form of QString
QString qt_file_name = file_path->text();
char *file_name;

for(int i=0;i<qt_file_name.length();i++)
{
// used each statement one by one
file_name[i] = static_cast<char>(qt_file_name.at(i).toAscii()); // causing segmentation fault
file_name[i] = static_cast<char>(qt_file_name.at(i).toLatin1()); //causing segmentation fault
}

Prashant_Bhutani
19th April 2010, 08:51
now I have replaced for loop with this line
strcpy(file_name,qt_file_name.toAscii());
but still the answer is same ,i.e, SEGMENTATION FAULT

Prashant_Bhutani
19th April 2010, 09:28
could anyone please tell what's the reason of segmentation fault
because according to Qt's documentation the above statements should work fine

whenever I am making the conversion statements a comment, the program is running fine
but as soon as I include those statements, all the havoc starts happening

Lesiok
19th April 2010, 11:27
Because You don't initialise file_name variable. If You realy need this conversion (I don't see any reason for this) You must do this like that

char *file_name = new char[200];
or

char file_name[200];
It is C++ abc-book :)