I'm asking for your help in handling accented letters in the console in Windows.
I'm writing a programme that needs to use user input, both filenames (via drag and drop) and text. Everything is fine if the filenames/text is ASCII, but if they contain accented letters like á, ő, or è then everything goes to hell.Here's a code snippet that saves text input in a file named text.txt to see if accented letters are corrupted and tests if it can find a file with funny characters in its name or path.
This works fine for me on Ubuntu with any input and it works on Windows with ASCII input, but it fails with accented letters on Windows XP. Files with characters like í in their name are not found and I just get stuff like \x82\xA0\xFB\xA3\xA1 in the output txt, along with "does not map to Unicode" errors. If I remove the binmode STDIN line, the only difference is that I get mojibake in the output file, but still nothing works.#!/usr/bin/perl use strict; use warnings; use utf8; binmode STDIN, ':encoding(UTF-8)'; print "Type some funny characters, they will be saved in text.txt\n"; chomp (my $text = <STDIN>); print "\n\nYou typed: $text\n"; # open (OUT, ">:encoding(UTF-8)", "text.txt") or die "Can't open file: + $!"; open (OUT, ">", "text.txt") or die "Can't open file: $!"; print OUT "$text\n"; close OUT; my ($inputfile_full, $folder, $inputfile, $inputfile_noext, $ext); do { print "\nDrag and drop the input file here and press enter.\n"; chomp ($inputfile_full = <STDIN>); # strip any leading and trailing spaces and single or double quote +s $inputfile_full =~ /^ *[\"\']?(.*)[\/\\]([^\"\']*)[\"\']? *$/; # $1 = everything up to last / or \, $2 = everything from there on + to the end except ",' and spaces at the end $folder = $1; $inputfile = $2; $inputfile =~ /(.*)\.(.*)/; $inputfile_noext = $1; $ext = $2; # strip quotes $inputfile_full =~ s/^ *[\"\']?([^\"\']*)[\"\']? *$/$1/; print "\nThe file doesn't seem to exist\nFilename: $inputfile\nPat +h: $folder\n\nTry again!\n\n" unless (-e "$folder/$inputfile"); }until (-e "$folder/$inputfile"); print "\nFile (${inputfile}) found.\nPath: $folder\nPress enter to con +tinue\n"; <STDIN>;
In reply to Character encoding in console in Windows by elef
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |