0

I want my program to take a wchar string from the user and print it to a file,but even though it print the string to the command prompt correctly,when it comes to the file it only prints the ascii characters, any other characters is printed incorrectly.

Example: instead of writing "olá" it prints "ol "

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main(){
    FILE *pst = fopen("C:\\teste1.txt","a");
    wchar_t word[100];
    fgetws(word,20,stdin);
    fputws(word,stdout);
    fputws(word,pst);
    fwprintf(pst,word);
    return 0;
}
  • 4
    close the file before leaving. But you need to say what you mean by 'doesnt work' – pm100 Jun 08 '18 at 18:23
  • There is no reason to think that your program would send different output to a file than it does to the standard output. But how you *check* the output could certainly differ. – John Bollinger Jun 08 '18 at 18:30
  • 3
    How do you *view* the file? – Eugene Sh. Jun 08 '18 at 18:31
  • well it's what happens, in the command prompt the string is correct, but in the file it isn't. – Paulo Cezar Sousa Paes Jun 08 '18 at 18:33
  • Also not clear to me why you are writing to the file *twice* using `fwprintf` and `fputws` – Eugene Sh. Jun 08 '18 at 18:33
  • 1
    Open how? With a can opener? – Eugene Sh. Jun 08 '18 at 18:34
  • I did that just to show that neither fwprintf nor fputws work – Paulo Cezar Sousa Paes Jun 08 '18 at 18:34
  • Like you open any other file or program, by double clicking it – Paulo Cezar Sousa Paes Jun 08 '18 at 18:35
  • 1
    Your doubleclick behavior is totally depending on your environment, which we have no idea about. If it is opening in Notepad, then there is no surprise you won't see some characters. – Eugene Sh. Jun 08 '18 at 18:37
  • @PauloCezarSousaPaes, myself, I open most programs by typing a shell command, not by clicking on anything. In any case, are you *intentionally* being difficult with the people trying to help you? The point we're trying to get at is that it matters which program you use to examine the file content, perhaps also that program's settings, and it may even depend on specific commands you execute in the program. – John Bollinger Jun 08 '18 at 18:39
  • If you want the locale to be taken into account, you need to do more than include ``. You need to start your program with `setlocale(LC_ALL, "");`. – rici Jun 08 '18 at 18:40
  • I had no intention of difficulting things, I didn't knew there was another way of opening a text file. i'm opening the file with notepad, and i didn't setlocale because when i set it to "Portuguese" for example and then proceed to print the string to the command prompt it wasn't displayed correctly – Paulo Cezar Sousa Paes Jun 08 '18 at 18:44
  • 1
    Can you *type* the same text in your Notepad? And then save, close and reopen it? – Eugene Sh. Jun 08 '18 at 18:48
  • The freeware [Notepad++](https://notepad-plus-plus.org/) is far superior to the basic Notepad. It supports various character encoding methods, and many character sets too, and text highlighting for many computer languages. – Weather Vane Jun 08 '18 at 18:48
  • Yes, if i type the text in notepad, save it, then open it it is displayed correctly – Paulo Cezar Sousa Paes Jun 08 '18 at 18:52
  • 1
    in summary - your problem is almost certainly not writing the file, it is the tool you are using to read it – pm100 Jun 08 '18 at 18:54
  • https://code.visualstudio.com/ – James Poag Jun 08 '18 at 18:55
  • When you pass a text file to a person or a program you have to tell which character encoding it uses. If you don't, it might guess instead of crying foul. – Tom Blodget Jun 08 '18 at 23:12

1 Answers1

1

The code posted behaves as it should.

Georgioss-MBP:~ gsamaras$ g++ -o m main.cpp 
Georgioss-MBP:~ gsamaras$ ./m
olá
olá
Georgioss-MBP:~ gsamaras$ cat test.txt 
olá
olá

Do that from a terminal, since the problem might be that you are viewing the file from a text editor, whose encoding is not set to display wide characters properly.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • You are correct, When i read the file from the terminal it works, thank you, although now i need to find a way for it to work with notepad – Paulo Cezar Sousa Paes Jun 08 '18 at 18:52
  • Good news @PauloCezarSousaPaes. I don't use notepad, so I can't help, but I am pretty sure that the internet has the answer to that. – gsamaras Jun 08 '18 at 18:53
  • The right thing would be *not* to use Notepad. Personally I use notepad to write short temporary notes for myself only. And even that is done pretty awkwardly with it, as it's "undo" depth is limited to 1.... – Eugene Sh. Jun 08 '18 at 19:32
  • @EugeneSh. Notepad++ is an easy upgrade and Paulo could consider trying it out. – gsamaras Jun 08 '18 at 20:53