简体   繁体   English

使用C ++创建和写入UTF-8文件

[英]Creating and writing to an UTF-8 file with C++

I have to create a m3u8 playlist in my http streaming C++ server code. 我必须在我的HTTP流C ++服务器代码中创建一个m3u8播放列表。 m3u8 is nothing but UTF-8 m3u file. m3u8就是UTF-8 m3u文件。

How do I create an UTF-8 file (ie how to write UTF-8 characters to a file)? 如何创建UTF-8文件(即,如何将UTF-8字符写入文件)? Maybe with the open() function or some other function in C++ on Linux? 也许使用open()函数或Linux上C ++中的其他函数?

int fd = open("myplaylist.m3u8", O_WRONLY | O_APPEND);

The way you are planning to do the open() is fine. 您计划执行open()方式很好。

What will determine if a file is in utf-8, is the characters you're going to write to it. 确定文件是否为utf-8的因素是要写入文件的字符。 Provided you encode the relevant characters as utf-8, everything will work as expected. 只要您将相关字符编码为utf-8,一切都会按预期进行。

If you plan on converting a given encoding (say ISO-8859-1) to utf-8, a good way to achieve it is to use libiconv which allows to do exactly that. 如果计划将给定的编码(例如ISO-8859-1)转换为utf-8,则实现该编码的一种好方法是使用libiconv ,它可以做到这一点。

Whether a file "is" utf-8 depends on the content. 文件是否为utf-8取决于内容。 As long as you write() the correct byte sequences in there you will be fine. 只要您在其中write()正确的字节序列就可以了。

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

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