Problem in conversion of QChar to char
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
Re: Problem in conversion of QChar to char
Please show us real code.
Re: Problem in conversion of QChar to char
// 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
}
Re: Problem in conversion of QChar to char
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
Re: Problem in conversion of QChar to char
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
Re: Problem in conversion of QChar to char
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
Code:
char *file_name = new char[200];
or
Code:
char file_name[200];
It is C++ abc-book :)