简体   繁体   English

C - 使用fprintf的奇怪行为

[英]C - Strange behavior with fprintf

I have a project where the program saves some messages into a log file. 我有一个项目,程序将一些消息保存到日志文件中。 At the start of my program, I open this file and functions write the text in it. 在我的程序开始时,我打开这个文件,函数在其中写入文本。 As I'm brazilian, we have special caractares like ç, ã and others. 因为我是巴西人,我们有像ç,ã和其他人一样的特殊功能。

The problem I'm having is that in one of the files, I have the following line: 我遇到的问题是,在其中一个文件中,我有以下行:

fprintf(logfile, "\nArquivo \"Instruções.txt\" criado\n");

and it works perfectly. 它完美无缺。 The log file is correct. 日志文件是正确的。

Another file, of the same project has the following line: 同一项目的另一个文件包含以下行:

fprintf(logfile, "Carregando configurações\n");

and the logfile is incorrect with 'çõ' instead of 'çõ'. 并且'çÃμ'而不是'çõ'的日志文件不正确。

It's important to notice that I didn't close the file and reopened in the meantime, it's the same from beginning to the end and the second line I've shown is the first command to write in the file. 重要的是要注意我没有关闭文件并在此期间重新打开,从开始到结束都是一样的,我显示的第二行是写入文件的第一个命令。

Another point is that if I copy that first line to the file of the second line, I get the same strange symbols, and the same problem if doing the opposite. 另一点是,如果我将第一行复制到第二行的文件中,我会得到相同的奇怪符号,如果相反则会出现同样的问题。

Now I don't have any idea what's going on. 现在我不知道发生了什么。 I've tried using %c and the number corresponding the characters (I use this method when using the console), but get the same problem. 我已经尝试使用%c和对应字符的数字(我在使用控制台时使用此方法),但是遇到了同样的问题。

Thanks very much! 非常感谢!

EDIT: I don't know if it was clear, but at the same log file, there's both correct and incorrect letters. 编辑:我不知道它是否清楚,但在同一个日志文件中,有正确和错误的字母。 It's juts different files of the project that have the commands. 它突出了具有命令的项目的不同文件。

这是因为您的编辑器用来显示您的日志文件的编码方法,配置您的编辑器使用UTF-8编码,应该没问题。

It looks like the log file is being interpreted as some old encoding. 看起来日志文件被解释为一些旧编码。 Nowadays, most things are written in UNICODE. 如今,大多数东西都是用UNICODE编写的。 In this case, C does not care what format your string is, but since most things are UTF-8 these days, it's unlikely that you ended up with any other kind of string. 在这种情况下,C不关心你的字符串是什么格式,但由于现在大多数东西都是UTF-8,所以你不太可能最终得到任何其他类型的字符串。

In this case, we can tell, because UTF-8 escape sequences look like this when not properly interpreted; 在这种情况下,我们可以看出,因为UTF-8转义序列在未正确解释时看起来像这样; When you improperly interpret something else as UTF-8, eg the other way around, you'll see UNICODE replacement characters instead. 如果您不正确地将其他内容解释为UTF-8,例如反过来,您将看到UNICODE替换字符。

If you are using Notepad and can't figure out a way to open the file with UTF-8, try another text editor. 如果您使用记事本并且无法找到使用UTF-8打开文件的方法,请尝试使用其他文本编辑器。 There's tons - gedit, Notepad++, etc. 有吨 - gedit,Notepad ++等。

Also: if you're wondering why it's all not gibberish, that's because UTF-8 is an ASCII-compatible encoding. 另外:如果你想知道为什么它不是乱码,那是因为UTF-8是一种与ASCII兼容的编码。 The first 128 characters are assigned the same in UTF-8 as ASCII. 前128个字符在UTF-8中与ASCII分配相同。 A lot of character sets share this property, or at least are close to it. 许多字符集共享此属性,或者至少接近它。

Edit: Upon further questions, it does appear something fishy is going on. 编辑:在进一步的问题,它确实出现了一些可疑的事情。 At the very least, though, it is definitely some weird encoding issue. 但至少,它肯定是一些奇怪的编码问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM