0

In C Programming; I cannot read files from a directory with Turkish characters, but if I change the name of the folder containing Turkish characters, I can read them.

  1. Os Name: Windows 10
  2. Programming Language: C
  3. IDE: Visual Studio Code

I have folder named Ülke. I cannot read files from this directory.

DIR *dr = opendir("C:/Users/softeng/tutorials/comp/Ülke/");

but When I rename this folder to Ulke, I can read it.

DIR *dr = opendir("C:/Users/softeng/tutorials/comp/Ulke/");

All Code;

#include <stdio.h>
#include <dirent.h>
#include <locale.h>
  
int main(void)
{
    setlocale(LC_ALL, "Turkish");
    struct dirent *de;
    DIR *dr = opendir("C:/Users/softeng/tutorials/comp/Ülke/");
  
    if (dr == NULL) {
        printf("Could not open current directory" );
        return 0;
    }
  
    while ((de = readdir(dr)) != NULL)
        printf("%s\n", de->d_name);

    closedir(dr);    
    return 0;
}

How can i solve it.

zoroglur
  • 395
  • 2
  • 18
  • Which compiler are you using? Also, did you define the function `opendir` yourself or is that part of some API you are using? – Andreas Wenzel Oct 10 '22 at 18:18
  • i use mingw64-gcc-g++, I have fully attached the Code I wrote. Can you check the Post again? – zoroglur Oct 10 '22 at 18:27
  • If you're using mingw64-gcc-g++, you can use C++ classes like `std::filesystem`. That understands wide characters like `L"C:/Users/softeng/tutorials/comp/Ülke/"`. Notice the leading L – MSalters Oct 10 '22 at 18:35
  • @MSalters How can I do it in C? – zoroglur Oct 10 '22 at 18:40
  • Probably have to use Win32 functions instead of emulated posix ones – Shawn Oct 10 '22 at 18:43
  • Looks like it's `FindFirstFileW()` and friends. See https://learn.microsoft.com/en-us/windows/win32/fileio/listing-the-files-in-a-directory – Shawn Oct 10 '22 at 18:45
  • @ResulZoroğlu: If there wa a C answer, I'd write it, but I commented to make sure you indeed need C instead of C++. It's a bit weird that you're using a C++ compiler for C. – MSalters Oct 10 '22 at 18:46
  • @MSalters There is no other reason why I chose to write a simple program. What do you recommend? – zoroglur Oct 10 '22 at 18:53
  • @ResulZoroğlu: If you know C++, obviously C++. [Example simple function](https://stackoverflow.com/a/32889434/15416). Just use `auto myPath=L"C:/Users/softeng/tutorials/comp/Ülke/";` – MSalters Oct 10 '22 at 18:56
  • @MSalters thank you for answer but i dont know C++, I only need to do directory read operations. but this is how i had the problem. I am researching how I can solve this situation in C. – zoroglur Oct 10 '22 at 19:00
  • *opendir* deals with ascii character strings (char *), whether `Ü` is an extended character set. You probably need to convert it to utf8/unicode format. There are 'c'-based functions around which do conversions, e.g., https://www.cprogramming.com/tutorial/unicode.html#:~:text=The%20most%20interesting%20one%20for,that%20refers%20to%20a%20character. You can try those or, as suggested earlier, use embedded c++ support for conversions. It might also be possible to set your code editor to treat all chars as unicode/utf8 chars, It might also resolve your issue. – Serge Oct 10 '22 at 19:31

2 Answers2

1

Vs Code >Preferences>Settings

Then;

Search

Files Encoding:

You can choose ISO-8859-9

And setlocale(LC_ALL="Turkish")

Ugur Baran
  • 108
  • 6
0

setlocale(LC_CTYPE, "en_US.UTF-8") setlocale(LC_CTYPE, "turkish") setlocale(LC_CTYPE, "iso-8859-9")

you can try these code.

Ugur Baran
  • 108
  • 6
  • I tried but didn't work – zoroglur Oct 11 '22 at 09:57
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 15 '22 at 01:29