-1

I have an ansi file with 'Ł' character I would like to read this character and save it to another file with the same encoding(to get 'Ł' character).

const fs = require('fs');
var content = fs.readFileSync('input.txt',null); //Ł
fs.writeFileSync('output.txt',content,null); //Ł how to get this?

How to do this with nodejs?

Update: I checked the input file:

file -i

text/plain charset=uknown-8bit

file -r

Non-ISO extended-ASCII, text with very long lines, with CRLF line terminators

Can I save the file with such details?

johnerfx
  • 679
  • 1
  • 8
  • 21
  • 1
    Define "ansi file" -- what **specific** encoding is the file using? When I try to save a file from Notepad with the "ANSI" option in the drop-down box (which I believe uses Windows-1252), it fails to save correctly, which makes me wonder if it has a mapping in that encoding... – T.J. Crowder Jan 25 '17 at 17:23
  • I open the file with notepad++, it says ANSI. – johnerfx Jan 25 '17 at 17:27
  • 1
    That code should work (regardless of encoding). When you don't give `readFile`/`readFileSync` an encoding, it reads a raw buffer (no conversion of data). When you give `writeFile`/`writeFileSync` a buffer, it doesn't use an encoding, it just writes the data. – T.J. Crowder Jan 25 '17 at 17:28
  • 1
    ^^ Verified, it happily copies files bit-for-bit. – T.J. Crowder Jan 25 '17 at 17:39
  • Yes, it actually works, I just need to figure out how to concatenate this character with the rest of my string and save it to file properly. – johnerfx Jan 26 '17 at 08:02

2 Answers2

0

I achieved what I wanted by creating binary strings out of buffer with the problematic characters:

const fs = require('fs');
function getChar(charId) {
    let characters = { AA:165, //Ą
         EE:202, //Ę
         CC:198, //Ć
         LL:163, //Ł
         SS:140, //Ś
         OO:211, //Ó
         ZZ:175, //Ż
         ZZZ:143, //Ź
         aa:185, //ą
         ee:234, //ę
         cc:230, //ć
         ll:179, //ł
         ss:156, //ś
         oo:243, //ó
         zz:191, //ź
         zzz:159 //ź
    };
    let buf = new Buffer({
        type:'Buffer',
        data:[ characters[charId] ],
        encoding:'ISO-8859-2'
    });
    return buf.toString('binary')
}
fs.writeFileSync('output.txt',getChar('LL')+" test",'ascii'); //'Ł test'
johnerfx
  • 679
  • 1
  • 8
  • 21
-1

It's most likely this is a utf-8 encoded file, rather than an "ansi" one. That 'Ł' character is not representable on the ansi encoding. Check with the file command on unix-es what sort of encoding it has and proceed from there.

Community
  • 1
  • 1
Horia Coman
  • 8,074
  • 2
  • 19
  • 23