Operation file binary mode and text mode &c operation file for Ansi,utf-8,unicode support

Source: Internet
Author: User

FILE * Pfilein;
Pfilein=fopen ("XXX.txt", "RT");

int __cdecl fgetc (REG1 FILE *stream);
char * __cdecl fgets (_tschar *string,int count,file *str);
and Fputc,fgets, fscanf,fprintf.

1. The above are all provided by C language File operation method, which has binary mode: B and text mode T.

Difference:

Binary mode, in fact, the hard drive inside the things moved to the memory, do not handle. Read the words (now default to ASCII characters, followed by a discussion of the differences between the different encodings)

For enter, the two ASCII characters are generated in memory (because they exist in the hard disk). \ r \ n

Why use two characters to represent an action, this is from the previous English typing machine operating mode is (first step) to the type of car first move to the side of the typing (carriage), (second step)

And then move down one line (line-breaking), which is split in half, so it's roughly considered a legacy of history.

Text mode, because we say the text, is human readable, then there is no need to use two steps to represent an action, the use of/n is finished. But the two models are causing

Confused, when I read in binary mode, saved in text mode will be a more than a \ r, when you open with EditPlus, you will find the following:

0D 0A before multiple 0D, Notepad ignores redundant 0D;

Similarly, open with text mode, save with binary, and then open with a text program, there is less than A/R, as follows:

And for such a malformed thing, Windows itself is not aware of Notepad:

And EditPlus after all is a professional tool, so it is possible, not on the map ...

2. The previous coding method is the system default encoding, ANSI it is a mixture, for English is ASCII, for Chinese characters is GB2312, our C language provides the above several ways to support this encoding only,

For Unicode,utf-8 and so on will appear garbled:

Ansi



Utf-8



Unicode



3.gets

The above is for reference only, may have a lot of deficiencies and errors, welcome to point out.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.