ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
How can I read and write text files (encoding: UTF8, Programming Lang: C, O.S.: Linux)? Is there any prepared source code? How about working with ‘\n’ or ‘\t’ … when the encoding is UTF8?
That's exactly what C does best; essentially UTF-8 *is* 7-bit ASCII
That's not correct. UTF-8 supports 7-bit ASCII but it also supports much more than that. Beyond the 7bit ASCII UTF-8 will be encoded using a standard algorithm.
To read any UTF-8 file (except in the trivial case when the file only contains 7-bit ASCII) then a UTF-8 library should be used.
Since I normally use C++ rather than C I use the Qt library, however I'm sure that there is a good library within the Gnome project that you could use. For the serious checkout IBM's ICU library.
UTF-8 supports 7-bit ASCII but it also supports much more than that.
<= This is absolutely true
I didn't want to overcomplicate things in my reply - but I hope I didn't oversimplify them, either (at least not too much ).
I want to emphasize - for the majority of string handling you're likely to do in C (and certainly for things like 8-bit '\n' and '\t' characters), you can essentially say "the standard C library gives you UTF-8 for free". Because the majority of string handling in most "classic" C programs *IS* 7-bit ASCII.
Please follow up on some of the links provided. They're all good. I'd especially recommend this one:
Thanks! I have written a similar code. However, it does not works. I want to read a Persian text. And after some simple modifications (such as finding tabs, points, ...), write to another file.
Quote:
Originally Posted by paulsm4
That's exactly what C does best; essentially UTF-8 *is* 7-bit ASCII:
PS:
You're already going UTF-8. That's definitely the right thing to do, and apologies again for confusing things. Here's a quote from the above link emphasizing this point:
Quote:
The best advice you can get is to steer clear of wide characters.
You should never need to use any wide character functions.
Keep the data in your program internally represented as utf-8.
The standard byte-oriented "strlen", "strcpy", "strstr", "printf" etc
work fine with utf-8.
XML uses utf-8 by default as well, so little if any conversion between
encodings should be needed. You may have to convert your input from a
legacy encoding to utf-8, or you could just externally convert using
something such as this:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.