When i try to create a file in LINUX using open() function, i get an error '-1' for the filename that contains extended character (ex: Björk.txt). Here the file contains a special character ö (ASCII 148)
I am using the below code:
char* szUnixPath
/home/user188/Output/Björk.txt
open(szUnixPath, locStyle, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
I always get a -1 error, and NO FILE is created.
As the OS encounters the ASCII 148, it throws an error.
The same function works perfectly fine if i use a tilde ~ (ASCII 126, example: Bj~rk.txt) or any other character below ASCII value 128.
can somebody explain why do i get the -1 error only for filename having special character ranging between 128-255 ?
I recommend just trying yourself to see what bytes this name contains.
Create the file in a directory, then run the following simple C program:
We can easily see in the output that 'Björk' length is 6-bytes. And we can see these bytes values:
Filenames in Linux are generally specified in UTF-8, not CP437. The
open
is failing because the filename you're passing doesn't match the one in the OS.Try opening this file instead:
/home/user188/Output/Bj\xc3\xb6rk.txt
. This is the special character encoded in UTF-8 as two bytes.