How to count the number of Chinese Characters in txt

Source: Internet
Author: User

I learned a file these two days and tried to write a simple program for counting the number of Chinese characters in the TXT file. For more information about encoding, see gb2132. The following is a program. please correct me.

# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
File * FP;/* define file pointer */
Char ch;
Int num = 0;
Fp = fopen ("E: \ wenzi.txt", "R");/* open the file location */
If (fopen = NULL)/* returns NULL due to an open Error */
{
Printf ("error to open the file \ n ");
System ("pause ");
Exit (0);/* exit normally */
}

Ch = fgetc (FP);/* Get A character */

While (Ch! = EOF)
{

Short int TEM = (short INT) ch;/* converts the value of the first byte */
If (TEM> =-80 & TEM <=-9)/* judge the first byte */
{
Fseek (FP,-1, seek_cur);/* the pointer returns a byte */
Ch = fgetc (FP);/* output this Chinese character */
Putchar (CH );
Ch = fgetc (FP );
Putchar (CH );
Printf ("\ t ");
Num ++;/* Chinese Character Count + 1 */
}
Ch = GETC (FP );
}
Printf ("\ n ");
Printf ("the number of Chinese characters is % d \ n ");

Rewind (FP);/* reset the pointer to the beginning */
Ch = fgetc (FP );
While (Ch! = EOF)/* the file ends with EOF */
{

Putchar (CH );
Ch = fgetc (FP );
}
Fflush (FP);/* refresh the buffer and output the files in the buffer zone to the disk */
Fclose (FP);/* close the file */
System ("pause ");

}

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.