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 ");
}