问题
I want to open a file with QFile::Open where my file name is unicode:
QString fname(QFile::decodeName("D:/أحدالأنشطة.txt"));
QFile qFile(fname);
bool b=qFile.open(QIODevice::ReadOnly);
if(b)
{
FILE* filedesc = fdopen(qFile.handle(), "rb");
if(filedesc!=NULL)
{
char* nb=(char*)malloc(2*sizeof(char));
qDebug()<<"opened ";
size_t size=fread(nb,sizeof(char),2,filedesc);
fclose(filedesc);
qDebug()<<"filedesc closed size "<<size<<"nb "<<QString::fromAscii(nb,2);
nb=NULL;
free(nb);
}else qDebug()<<"filedesc failed error"<<strerror(errno);
}else
qDebug()<<"qFile failed error"<<strerror(errno);
It failed and I get:
qFile failed error No error
any help will be appreciated.
回答1:
If the data is in WCHAR
array than just use QString filename((QChar*) yourWcharData);
回答2:
If your source file is UTF-8 encoded, then you might be able to do this:
QString fname(QString::fromUtf8("D:/أحدالأنشطة.txt"));
If it's UTF-16, then:
QString fname(QString::fromUtf16("D:/أحدالأنشطة.txt"));
If the source file is neither UTF-8 not UTF-16, try this instead:
QString fname(QString::fromLocal8Bit("D:/أحدالأنشطة.txt"));
If that also doesn't work, then you need to find out the character set your editor is using.
来源:https://stackoverflow.com/questions/13413453/qfileopen-fails-with-unicode-filename