2

This question wa also posted in this SO post.

I have a standard text file that is shown perfect with cat:

$ cat myfile.txt
v0[shape = Mrecord, label = ""]

When I use xxd to examine the exact line ending I get this:

$ xxd myfile.txt
00000000: 7630 5b73 6861 7065 203d 204d 7265 636f  v0[shape = Mreco
00000010: 7264 2c20 6c61 6265 6c20 3d20 2222 5d0a  rd, label = ""].

However when I try to open it with gedit, I get a weird encoding of some non Latin language. What goes wrong in gedit? Here is the complete source code for reproducing:

$ cat main.cpp
#include <fstream>
int main(int argc, char **argv)
{
    std::ofstream myfile("myfile.txt");
    myfile << "v0[shape = Mrecord, label = \"\"]\n";
    myfile.close();
    return 0;
}

And here is my compilation line:

g++ main.cpp -o main

In addition, I'm attaching the image file of the the gedit screen:

enter image description here

EDIT: (output from suggested remarks)

$ gedit --list-encodings | grep -Fx UTF-8
UTF-8
$ gedit --encoding UTF-8 myfile.txt
$ file -bi myfile.txt
text/plain; charset=us-ascii
  • 1
    Just to get it right: When you open myfile.txt in gedit it just shows those asian characters instead of v0[shape = Mrecord, label = ""]? For me it works, btw (with myfile.txt created by your C++ program). What does file myfile.txt show? – PerlDuck Aug 1 '18 at 9:57
  • pretty sure it's a language issue, it's inside the Tools menu, Defined language, just select English – damadam Aug 1 '18 at 10:08
  • @PerlDuck yes, when I open myfile.txt it shows those Asian characters instead of what it should. I'm on Ubuntu 17.10 with the default gedit shipped. – OrenIshShalom Aug 1 '18 at 10:24
  • @damadam English was already highlighted as the chosen language. – OrenIshShalom Aug 1 '18 at 10:56
  • 3
    @OrenIshShalom 17.10 is EOL – damadam Aug 1 '18 at 11:33
3
+50

You can force the encoding to UTF-8 when you launch the gedit, e.g.,

\gedit --encoding UTF-8 myfile.txt

Somewhere you have the default encoding set to UTF-16LE as indicated by @filipe-brandenburger. You can verify this by using the UTF-16LE encoding to see the weird output.

\gedit --encoding UTF-16LE myfile.txt

Update -- encoding may be stored with the file uri.

Gnome editor apparently cached the encoding type along with the file uri. You can see this my moving the file around. E.g.,

echo "Hello there." > testfile.txt
\gedit --encoding UTF-16LE testfile.txt

(see Chinese text)

mkdir tempdir
mv testfile.txt tempdir
cd tempdir
\gedit --encoding UTF-8 testfile.txt

(English text)

Now open without specifying encoding.

\gedit testfile.txt

(English text)

But moving the file back cause it to use a different encoding.

mv testfile.txt ..
cd ..
\gedit testfile.txt

(see Chinese text)

  • Why are you putting a backslash in front of gedit? it should run fine without the backslash. – WinEunuuchs2Unix Aug 9 '18 at 0:45
  • @WinEunuuchs2Unix I added the backslash just in case there is an alias for gedit. The backslash remove any alias for it. – Bernard Wei Aug 9 '18 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.