在C中显示特殊字符
我有一个要求,C 代码从数据库中提取字符串数据并将其写入文件。数据库中的字符串数据可以有任何类型的字符
例如:描述字段有数据“Adj \342\200\223 Data”,当我写入文件时它写入的文本为“Adj â Data”。同样,这个描述字段可以有任何类型的数据,我的代码只是在从数据库中提取并写入文件后读取并使用 strcpy 。
如何将描述字段中的数据写入文件?
I have an requirement where the C code extract string data from database and write it to a file. The string data in the database can have any kind of characters
for example: Description field have data "Adj \342\200\223 Data" , when I write to the file the text it writes as "Adj â Data". Similarly, this description field can have any kind of data, my code just read and uses strcpy after extracting from the database and write to a file.
How do I get the data written to a file as it is in the description field ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
认为最简单的解决方案是逐字节写入 - 与缓冲无关:
编辑:
可能误解了你的问题。仅在实际使用字符串时才使用字符串函数。对于二进制数据,请使用二进制函数(例如提到的
memcpy()
)。编辑2/3:
不要将值打印为“%d”或“%u” - 应为“%3o”以打印为 3 位八进制数。如果后面跟着其他数字,则使用“%o”可能不安全。
Think easiest solution would be writing byte by byte - shouldn't matter that much with buffering:
Edit:
Might have misunderstood your question. Only use string functions when you're actually working with strings. For binary data use binary functions (e.g. the mentioned
memcpy()
).Edit 2/3:
Don't print the value as "%d" or "%u" - should be "%3o" to print as a 3-digit octal number. Using "%o" could be unsafe if other digits follow.