Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString to wchar_t* conversion
Forum Updated to NodeBB v4.3 + New Features

QString to wchar_t* conversion

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 850 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sogo
    wrote on last edited by
    #1

    Hi,
    I am having some confusion regarding conversion from QString to wchar_t* using QString::toWCharArray(wchar_t* inputFileLocation). I am doing this conversion to get location of a file and reading it using C api "_wfopen" and "fread" but it sometimes it is not able to open and read file. Here is a little snipper of my code of what I am doing:

    QString inFile = "<Some file location is OS dir>";
    wchar_t* iFile = new wchar_t[inFile.size()];
    inFile.toWCharArray(iFile);
    FILE* openFile = _wfopen(iFile, L"rb");
    if (openFile == NULL) { qDebug() << "Can not open input file"; throw 2; }
    

    Now this behavior was happening very randomly as in sometimes it will open and sometimes it doesn't so I looked online and saw this forum post
    conversion issue from QString-wchar_t - stackoverflow
    and saw the third post from jake_w that this conversion does not terminate the array with null. I just wanted to confirm whether this conversion terminates the array with null or do I have to do it manually like below:

    QString inFile = "<Some file location is OS dir>";
    wchar_t* iFile = new wchar_t[inFile.size()+1];
    inFile.toWCharArray(iFile);
    iFile[inFile.size()] = '\0'; //Manually terminate array
    FILE* openFile = _wfopen(iFile, L"rb");
    if (openFile == NULL) { qDebug() << "Can not open input file"; throw 2; }
    
    jsulmJ JonBJ 2 Replies Last reply
    0
    • S sogo

      Hi,
      I am having some confusion regarding conversion from QString to wchar_t* using QString::toWCharArray(wchar_t* inputFileLocation). I am doing this conversion to get location of a file and reading it using C api "_wfopen" and "fread" but it sometimes it is not able to open and read file. Here is a little snipper of my code of what I am doing:

      QString inFile = "<Some file location is OS dir>";
      wchar_t* iFile = new wchar_t[inFile.size()];
      inFile.toWCharArray(iFile);
      FILE* openFile = _wfopen(iFile, L"rb");
      if (openFile == NULL) { qDebug() << "Can not open input file"; throw 2; }
      

      Now this behavior was happening very randomly as in sometimes it will open and sometimes it doesn't so I looked online and saw this forum post
      conversion issue from QString-wchar_t - stackoverflow
      and saw the third post from jake_w that this conversion does not terminate the array with null. I just wanted to confirm whether this conversion terminates the array with null or do I have to do it manually like below:

      QString inFile = "<Some file location is OS dir>";
      wchar_t* iFile = new wchar_t[inFile.size()+1];
      inFile.toWCharArray(iFile);
      iFile[inFile.size()] = '\0'; //Manually terminate array
      FILE* openFile = _wfopen(iFile, L"rb");
      if (openFile == NULL) { qDebug() << "Can not open input file"; throw 2; }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @sogo said in QString to wchar_t* conversion:

      I just wanted to confirm whether this conversion terminates the array with null or do I have to do it manually like below:

      It's mentioned in the documentation: https://doc.qt.io/qt-5/qstring.html#toWCharArray
      "Note: This function does not append a null character to the array."...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • S Offline
        S Offline
        sogo
        wrote on last edited by
        #3

        Oh, I missed that line. I will then append it manually. Thanks and let me close this issue.

        1 Reply Last reply
        1
        • S sogo

          Hi,
          I am having some confusion regarding conversion from QString to wchar_t* using QString::toWCharArray(wchar_t* inputFileLocation). I am doing this conversion to get location of a file and reading it using C api "_wfopen" and "fread" but it sometimes it is not able to open and read file. Here is a little snipper of my code of what I am doing:

          QString inFile = "<Some file location is OS dir>";
          wchar_t* iFile = new wchar_t[inFile.size()];
          inFile.toWCharArray(iFile);
          FILE* openFile = _wfopen(iFile, L"rb");
          if (openFile == NULL) { qDebug() << "Can not open input file"; throw 2; }
          

          Now this behavior was happening very randomly as in sometimes it will open and sometimes it doesn't so I looked online and saw this forum post
          conversion issue from QString-wchar_t - stackoverflow
          and saw the third post from jake_w that this conversion does not terminate the array with null. I just wanted to confirm whether this conversion terminates the array with null or do I have to do it manually like below:

          QString inFile = "<Some file location is OS dir>";
          wchar_t* iFile = new wchar_t[inFile.size()+1];
          inFile.toWCharArray(iFile);
          iFile[inFile.size()] = '\0'; //Manually terminate array
          FILE* openFile = _wfopen(iFile, L"rb");
          if (openFile == NULL) { qDebug() << "Can not open input file"; throw 2; }
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @sogo
          Given that you are using Qt, why do you want the conversion to wchar_t? You are using it in order to use the FILE type and do your I/O with calls like _wfopen(), doubtless under Windows. I cannot see any reason you would want to do that. Given that you are writing a Qt application, you would be much better off using Qt file access functions, for all sorts of reasons.

          S 1 Reply Last reply
          0
          • JonBJ JonB

            @sogo
            Given that you are using Qt, why do you want the conversion to wchar_t? You are using it in order to use the FILE type and do your I/O with calls like _wfopen(), doubtless under Windows. I cannot see any reason you would want to do that. Given that you are writing a Qt application, you would be much better off using Qt file access functions, for all sorts of reasons.

            S Offline
            S Offline
            sogo
            wrote on last edited by
            #5

            @JonB Good point, we have kind of big project and almost all parts of it uses C styles api and we recently started with Qt so to have consistency, just going for IO using c style. In the future we port it to qt or c++. probably.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bonnie
              wrote on last edited by
              #6

              Wouldn't inFile.toStdWString().c_str() be a better choice even if you need to call _wfopen()?

              S 1 Reply Last reply
              0
              • B Bonnie

                Wouldn't inFile.toStdWString().c_str() be a better choice even if you need to call _wfopen()?

                S Offline
                S Offline
                sogo
                wrote on last edited by
                #7

                @Bonnie Hi Bonnie, how will that work? Wouldn't be return type of inFile.toStdWString().c_str() be char* array and _wfopen does not take that as argument. Plus it will not serve purpose of storing array characters in 2 byte.

                B 1 Reply Last reply
                0
                • S sogo

                  @Bonnie Hi Bonnie, how will that work? Wouldn't be return type of inFile.toStdWString().c_str() be char* array and _wfopen does not take that as argument. Plus it will not serve purpose of storing array characters in 2 byte.

                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by
                  #8

                  @sogo
                  It is toStdWString, not toStdString :)

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved