The program loops, because the fgetws() function returns NULL both at the end of the file and in the case of any error (in this case, errno = 84: Invalid or incomplete multibyte or wide character ). The read pointer in the file remains in place, i.e. end of the file you never reach.
For verification, you can add :
else if (ferror(fp)) err(2, "fgetws errno = %d", errno);
after
if (fgetws(arrayofword[i], 63, fp) != NULL){ ... }
This error occurs because the locale "Russian" unknown to the system. In your case setlocale() returns NULL, and this error corresponds to - errno = 2 setlocale: No such file or directory .
In general, for the program to work, it is enough to set a suitable locale:
avp@avp-ubu1:hashcode$ locale LANG=en_US.UTF-8 LANGUAGE=en LC_CTYPE="en_US.UTF-8" LC_NUMERIC=en_SG.UTF-8 LC_TIME=en_SG.UTF-8 LC_COLLATE="en_US.UTF-8" LC_MONETARY=en_SG.UTF-8 LC_MESSAGES="en_US.UTF-8" LC_PAPER=en_SG.UTF-8 LC_NAME=en_SG.UTF-8 LC_ADDRESS=en_SG.UTF-8 LC_TELEPHONE=en_SG.UTF-8 LC_MEASUREMENT=en_SG.UTF-8 LC_IDENTIFICATION=en_SG.UTF-8 LC_ALL= avp@avp-ubu1:hashcode$
call setlocale(LC_ALL, ""); and correct in your output the format to L"%ls\n" (as explained in the answer @Roman Khimov),
and also to analyze the result of calling functions and correctly respond to errors .
PS
Since locales are not always set, and functions for working with wchar_t sometimes react extremely painfully to errors in the input data (that is Invalid or incomplete multibyte or wide character ), and moreover, for example, the output of printf / wprintf functions does not work. ,
I prefer to work with UTF directly (since this is just a sequence of bytes ( char * ), in most cases standard printf , scanf , strcmp , etc. is enough), i.e. without translating into wchar_t . If a translation is required in wchar_t (UCS) (and vice versa, as well as some specific functions), then I use my functions (from ucsutf.h , ucsutf.c ) that do not depend on setlocale () .