Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8826229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:05:10+00:00 2026-06-14T07:05:10+00:00

I’m trying to open a file to treat it later. My problem is that

  • 0

I’m trying to open a file to treat it later. My problem is that if my file name is not ANSI (Arabic, Hindi…) fopen_s and fopen refuse to open it and give me an Invalid argument error. I can’t use CreateFile() to do that so I thought to check either my file name is supported by fopen or not(try to open it) and create a temporary file instead:

QString fileN=QString::fromWCharArray(fname); 
QFileInfo file(DIRPath+"/"+fileN);
bool Supported=true;
if(file.exists()) {
    QString temp;   
    char* Fname=(char*)malloc(260*sizeof(char));
    strcpy(Fname,(QString(DIRPath+"/"+fileN).toStdString()).c_str());
    FILE* Filedesc;
    errno_t err=fopen_s(&Filedesc,Fname,"rb");
    if(Filedesc!=NULL) {
        qDebug()<<"\nfile opened ";
        fclose(Filedesc);
    } else if(err==22) {
        qDebug()<<"\nfail to open file error 22: Invalid argument";
        temp=QString(DIRPath+"/Temp"+QString::number(nb));
        Supported=false;
    } else qDebug()<<"\nfail to open file error"<<GetLastError()<<"errno"<<errno<<"strerrno"<<strerror(errno);
    Fname=NULL;
    free(Fname);
    ...

My question is: can anyone clarify for me the UNICODE/ANSI confusion? Am I safe so far or are there more precautions to consider? Is there a safer way to check if the given name is not ANSI?

Thank you in advance, any help will be appreciated.

EDIT 1

I tried this but in vain : CreateFile() return an INVALID_HANDLE_VALUE and GetLastError() return 0

//WCHAR fname[]=L"D:/أحدالأنشطة.txt";
char* name="D:/أحدالأنشطة.txt";
wchar_t* nameW=(wchar_t*)malloc(sizeof(wchar_t)*17);
qDebug()<<"s :"<<mbstowcs(nameW,name,17);
//QString path=QString::fromWCharArray(fname,17);
//QString path=QString::fromLatin1(name,17);

HANDLE fileHandle = CreateFile(    nameW,                 // file to open
                                   GENERIC_READ,          // open for reading
                                   FILE_SHARE_READ,       // share for reading
                                   NULL,                  // default security
                                   OPEN_EXISTING,         // existing file only
                                   FILE_ATTRIBUTE_NORMAL, // normal file
                                   NULL);

if (fileHandle == INVALID_HANDLE_VALUE)
    {
      qDebug()<<"CreateFile failed!\n"<<GetLastError();
      nameW=NULL;
      free(nameW);
      return 2;
     }else
          qDebug()<<"CreateFile succeeded!\n";

int fd = _open_osfhandle((intptr_t) fileHandle, _O_RDONLY);
FILE* fstr = _fdopen(fd, "r");
QFile indirect;
if (!indirect.open(fstr, QIODevice::ReadOnly))
    qDebug()<<"QFile open against file descriptor failed!\n";
    else
        {
          qDebug()<<"QFile open against file descriptor succeeded!\n";                    
          indirect.close();
        }

// This will fail 
QFile direct(path);
if (!direct.open(QIODevice::ReadOnly))
    qDebug()<<"QFile open of filename directly failed!\n";
    else
    {
     qDebug()<<"QFile open of filename directly succeeded!\n"; 
     direct.close();
    }
   nameW=NULL;
   free(nameW);

EDIT 2

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:05:11+00:00Added an answer on June 14, 2026 at 7:05 am

    You should probably use QFile to open the file, and then pass QFile::handle() to your C function. In the C code you would then use fdopen() to associate a FILE* stream to the file descriptor. Note that the mode you use in fdopen() should be compatible with the mode you used in QFile::open(). For example:

    void c_func(int fd)
    {
        FILE* file = fdopen(fd, "rb");
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.