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. Open file with ostream with file name from QFile?
Qt 6.11 is out! See what's new in the release blog

Open file with ostream with file name from QFile?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.0k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    greatbunzinni
    wrote on last edited by
    #1

    I want to output some text by using C++'s ostream but it appears that QFile::fileName() mangles the text string. Is there a way to open a file through ofstream through the string provided by QFile::fileName()?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vinb
      wrote on last edited by
      #2

      Hi,
      Mayby you can use "qtextstream":http://doc.qt.nokia.com/4.7/qtextstream.html or "qdatastream":http://doc.qt.nokia.com/4.7/qdatastream.html ?

      As for your question. Its possible to pass qfile::filename() to the open call from ofstream.
      You only have to add .toascii or .tolatin1 to it.
      Like:
      @
      ofstream ofstr;
      QFile f("path_to_file");
      fstr.open(f.fileName().toAscii());
      @

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #3

        vinb: You usually want to use "toLocal8Bit()" for conversions like that. Both toAscii and even toLatin1 do not work well when a user has non-Latin characters in his file names.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vinb
          wrote on last edited by
          #4

          thanks for correcting me, Tobias Hunger. :)

          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