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. How to save file in specific encoding?
Forum Updated to NodeBB v4.3 + New Features

How to save file in specific encoding?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 5.2k Views 3 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #6

    Not recognized then:

    for(auto codecstr: QTextCodec::availableCodecs())
                if(codecstr.startsWith("windows-"))
                    qInfo() << codecstr;
    

    Apparently it starts with lower case. Man there are a LOT of codecs in there (806 on linux).

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      Yet Zio
      wrote on last edited by
      #7

      I tried several aliases before too and with Other encodings for with setCodec(codecName)., it doesn't work even if its lower-case. You can try for yourself.

      ```Securing is more important than developing```

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #8

        Please provide a minimal testcase.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          Yet Zio
          wrote on last edited by
          #9
          // This is the code used for save method.
          QString fileName;
              // If we don't have a filename from before, get one.
              if (currentFile.isEmpty()) {
                  fileName = QFileDialog::getSaveFileName(this, "Save");
                  currentFile = fileName;
              } else {
                  fileName = currentFile;
              }
              QFile file(fileName);
              if (!file.open(QIODevice::WriteOnly)) { // If i use QIODevice::Text, then it can't handle line endings.
                  warnMsgBox *openWarnBox = new warnMsgBox(this);
                  openWarnBox->setText("Cannot save file");
                  openWarnBox->setDetailedText(file.errorString());
                  QSpacerItem* warnhorizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
                  QGridLayout* warnlayout = (QGridLayout*)openWarnBox->layout();
                  warnlayout->addItem(warnhorizontalSpacer, warnlayout->rowCount(), 0, 1, warnlayout->columnCount());
                  openWarnBox->exec();
                  return;
              }
              QFileInfo fileInfo(file);
              ui->tabWidget->setTabText(ui->tabWidget->currentIndex(), fileInfo.fileName());
              CodeEditor* tempEditor = (CodeEditor*)ui->tabWidget->widget(ui->tabWidget->currentIndex());
              tempEditor->changesSet = false;
          
              QTextStream out(&file);
              QByteArray tempCodec;
              tempCodec.append(tempEditor->codecToUse);
              QTextCodec *fileCodec = QTextCodec::codecForName(tempCodec);
              out.setCodec(fileCodec);
              out << tempEditor->toPlainText();
              file.close();
          

          The codecToUse is a personal variable used for knowing the encoding used for current editor from when the file was opened. This is then set to save the file with QTextStream output with codecToUse.

          If this was a problem with codecToUse, then i tried manually with "windows-1250" and others such as "ISO-..." and still the same prob

          ```Securing is more important than developing```

          1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #10

            Make sure you are writing non trivial text that contains encoded characters. If not it will not have anything to determine what the file type is. Even then it might get it wrong. I inserted an emoji into the text to get a linux program called "uchardet" to check file types. If I didn't include an emoji in the file it would always return ASCII. If I insert an emoji it will return UTF-8 when I encode it UTF-8. If I encode windows-1250 the program returns ISO-8859-3. I have no idea what that means. However, the encoding it definitely different if non trivial data is in the file.

                    QFile testfile("output.txt");
                    if (testfile.open(QFile::WriteOnly | QFile::Truncate)){
                        QTextStream out(&testfile);
                        out.setCodec("windows-1250");
                        //out.setCodec("UTF-8");
                        qInfo() << out.codec()->name();
                        out << "💩Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
                        out << "\n";
                        out << "💩Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
                        out << "\n";
                    }
            

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            2
            • Y Offline
              Y Offline
              Yet Zio
              wrote on last edited by
              #11

              Thanks. However I can convert characters easily between encodings, it seems there's no way to figure out the exact encoding which was used to write the file.

              ```Securing is more important than developing```

              1 Reply Last reply
              1
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @Yet-Zio said in How to save file in specific encoding?:

                there's no way to figure out the exact encoding which was used to write the file.

                no, since e.g. ascii is a subset of utf-8 and others also have common stuff so how should it be possible.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                Pablo J. RoginaP 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @Yet-Zio said in How to save file in specific encoding?:

                  there's no way to figure out the exact encoding which was used to write the file.

                  no, since e.g. ascii is a subset of utf-8 and others also have common stuff so how should it be possible.

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #13

                  @Christian-Ehrlicher said in How to save file in specific encoding?:

                  no, since e.g. ascii is a subset of utf-8

                  you could eventually detect the BOM for Unicode encoded files

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • Pablo J. RoginaP Pablo J. Rogina

                    @Christian-Ehrlicher said in How to save file in specific encoding?:

                    no, since e.g. ascii is a subset of utf-8

                    you could eventually detect the BOM for Unicode encoded files

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #14

                    @Pablo-J-Rogina But it's not mandatory. If there is a BOM then QTextStream will respect it.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #15

                      Maybe to extend on what @fcarney has said: The encoding of a file is usually not stored in the file itself and needs to be guessed. Even for UTF8 the BOM (which would help with detection) is optional. Most text editors look at the full text and then try to guess the encoding. So, one question would be if the text editor you are using for verification is able to guess your encoding.

                      Then, obviously, the text file you are looking at needs something in there to be able to distinguish encodings. If you use plain ASCII characters (only 128 characters, the first 128 in Unicode) then the editor is allowed to guess any encoding it wants. So, in order for us to be able to help you should also show the text you are trying to save.

                      One last thing is that I am not sure how Qt would handle text (which is Unicode internally) with characters that are not in the encoding you are using for saving. One possibility would be that it just writes the unicode character as UTF8 (though I don't know). In that case it could happen that the editor guesses UTF8 again (though I find this unlikely).

                      Anyway, you should create text specific to the encoding you are trying out. You cannot have one text (hopefully with non-ASCII characters as otherwise it would be a futile exercise) and try to save it with any encodings just for fun. Encoding and text have to be a match.

                      1 Reply Last reply
                      1

                      • Login

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