C language reads Chinese characters in a txt file and outputs garbled characters

**Record the common problems you encounter in file operations**

The case of garbled output

When using C language, we generally use fopenfunctions to open files, as follows

#include<stdio.h>
int main()
{
    
    
	FILE *fp;
	char a[10];
	fp=fopen("temp.txt","r");
	fscanf(fp,"%s",a);
	printf("%s\n",a);
	return 0;
}

If the output is all garbled characters
, open the corresponding temp.txt text file, select the file in the upper left corner to save it as, change the encoding from UTF-8 to ANSI at the bottom and save it, and then output the text normally (be careful not to memory space exceeded).

Related operations of fopen function reading and writing

fopenSome operation applications of functions

fopen("***.txt","w");

When using fopenthe function to open a file, if you want to continue writing data on the original basis, use awill wclear the original data in the document first, and a is to add data at the end of the document, and the pointer points to the end of the document.
It can also be used r+, r+and w+both are read-write modes, the difference is that w+the file will be cleared first.

Guess you like

Origin blog.csdn.net/Alkaid_J/article/details/112604371