-3

I'm making a program in C for a project where it as to print to a file everything it prints to the console. The problem is that I have to print some special characters like 'Ç', so I use the ascii codes and it printd fine to the console however what it prints to the file is incorrect. Here is an example:

printf(" %c", 128);

output to console: Ç

fprintf(output, " %c", 128);

output to file: €

I ran the command chcp in cmd and it tells me I'm using code page 850 and I used those asci codes, so I don't know what is the problem. The program writes to a notepad txt file.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Pedro
  • 63
  • 9
  • 3
    Check the codepage used by your editor – rettichschnidi Jan 14 '17 at 19:24
  • How do you know what character is stored inside your file? What makes you think that it is somehow caused by `fprintf`? It is most likely caused by the program that you use to inspect your file. – AnT stands with Russia Jan 14 '17 at 19:29
  • 3
    Note that `128` is not an ASCII code; ASCII only defines codes 0..127. – Jonathan Leffler Jan 14 '17 at 19:29
  • Where somebody see at source non ascii symbols? – oklas Jan 14 '17 at 19:30
  • What is the reasons to devote, can somebody clarify? – oklas Jan 14 '17 at 19:32
  • 1
    How are you analyzing the 'output to file'? What command are you using to display the contents of the file? CP850 has Ç at code-point 128; CP1252 has € at code-point 128. That's two different interpretations of the same 8 bits. – Jonathan Leffler Jan 14 '17 at 19:33
  • @AnT I know the problem is notepad, I just don't know how to solve it – Pedro Jan 14 '17 at 19:35
  • 1
    Maybe you need to tell Notepad to use CP850 code set instead of CP1252 — or vice versa. – Jonathan Leffler Jan 14 '17 at 19:36
  • @Rodrigo Pina: What problem are you talking about? The original problem of output diference between `printf` and `frpintf` does ot exist - both functions output the same thing. And console display will always be different from "Notepad" display - this is natural and cannot be "solved". – AnT stands with Russia Jan 14 '17 at 19:37
  • @JonathanLeffler when I open the text file it shows a '€' instead of a 'Ç' but in the console it shows a 'Ç' so I know that notepad is using a different codepage, and I used the command to change the codepage of the console but I don't think it is doing anything because the console output did not change – Pedro Jan 14 '17 at 19:37
  • Try to cat your file to console and say to us what simbol you see. – oklas Jan 14 '17 at 19:38
  • @oklas It's windows; it doesn't have a `cat` command. – melpomene Jan 14 '17 at 19:38
  • In windows use `more` or `less` instead of `cat`. Full command will be `more file.txt` Another windows solution is `copy file.txt con` – oklas Jan 14 '17 at 19:40
  • 1
    Note also this is undefined behavior. `%c` is the format specifier for a `char`, but `128` is an `int`. – Andrew Henle Jan 14 '17 at 19:41
  • 1
    @AndrewHenle But `char`s are promoted to `int` anyway when passed to a vararg function such as `printf`, so `%c` really expects `int` anyway. – melpomene Jan 14 '17 at 19:44
  • @oklas with the more comand it prints normally – Pedro Jan 14 '17 at 19:44
  • @oklas Windows doesn't have `less` either. It does have `type`, though. – melpomene Jan 14 '17 at 19:45
  • @Rodrigo Pina what you mean normally? same as print to console? – oklas Jan 14 '17 at 19:47
  • @oklas yes the same as the console – Pedro Jan 14 '17 at 20:23
  • @AndrewHenle Using `"%c"` is not UB with 128. The character associated with `(unsigned char) 128` is printed. "**c** ... the `int` argument is converted to an `unsigned char`, and the resulting character is written" C11dr §7.21.6.1 8 – chux - Reinstate Monica Jan 14 '17 at 20:49

2 Answers2

3

Ç is 128 in code page 437 or 850 etc., encodings which are sometimes used by Windows consoles. The same code 128 is in code page 1252 or 1250, encodings which are quite often used by Windows graphical applications. The only reasonable way to proceed is to have your consoles use the same encoding as the graphical applications; for this, you can use the command chcp 1252 (change code page) in the console at the command prompt.

(Note: for chcp to be effective, the console must use a TrueType font such as Lucida Console or Consolas.)

AlexP
  • 4,370
  • 15
  • 15
  • I used the command chcp 1252, but it is not working. Do I have to restart the system or the applications so that the changes take effect? – Pedro Jan 14 '17 at 19:40
  • 1
    It should be instantly obeyed in the console. Ah! I forgot, you must set the console font to a TrueType font, such as Lucida Console or Consolas. Apologies. – AlexP Jan 14 '17 at 19:41
  • ok thank you for the help – Pedro Jan 14 '17 at 20:23
1

Your command line (console) and whatever you use to display the file use different encodings. Both times the byte 128 is written, but in some Extended ASCII variant (see also Wikipedia) it is interpreted as a C with cedilla, whereas a common Windows encoding interprets it as the Euro symbol.

Hermann Döppes
  • 1,373
  • 1
  • 18
  • 26