QFile::open fails with unicode filename

一世执手 提交于 2019-12-11 06:48:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!