The reason and analysis of garbled characters when reading the content of the file with the file function, file problem (1)

When we are writing some projects, we need to use files to obtain and store data. As beginners when manipulating files, we will inevitably have some maddening problems, such as the title. Questions, I’m here to share with you some of the cause analysis and solutions of the problems I have encountered, to help you avoid pits and help you learn better~~~

When we write code in C, the file (binary file) is generally opened like this:

#include<stdio.h> 

int main(){
    
    
FILE *fp;

if((fp=fopen("D:\\file\\ff.dat","rb+"))==NULL){
    
    
 printf("can't open!");
 exit(0);//结束程序
 }

else{
    
    
...
}

After opening the file, we can rely on the file pointer to read and write the file, and sometimes use the **fread();** function to read the data and then output it, and find the data in the good file, and the output actually appears garbled! As follows:

Insert picture description here

There may be many reasons for this kind of result, we need to debug and eliminate one by one:
  1. To test whether it is a garbled code caused by the compiler not supporting Chinese output , you can use printf (" Hello! "); such a sentence test that directly outputs Chinese.
    If there are garbled codes, it is necessary to debug the unity between the system code and the compiler code. If the codeblocks integrated development environment is used, please refer to: Causes and solutions for Chinese garbled codes in codeblocks

  2. If your file is a text file (.txt), you can directly use Notepad to check whether the content exists and is correct.

  3. Check whether the operation of your writing function is wrong . Generally speaking, you cannot open the file manually for writing. You should use the file writing function such as ( fputs (); fscanf (); fwrite ();) to write information Import the file, and then use the relative file to read out the data such as ( fgetc () ; fprintf (); fread ();) and so on.
    Data read and write use certain format processing and it is not easy to cause problems. Random use may cause extra data such as a space in a data block. Then when using the function to read, it will appear after the data block When a data block is taken out, the space will occupy a position, which will cause the problem that the subsequent data is not correct. This leads to a problem, we can choose to refresh the file, re-write the new data strictly, and read it out again.

  4. Check whether the function that writes a block of data does not close the file after writing the data .
    If the file is not closed, the data just written does not enter the file, but temporarily stored in the buffer , then there is no data in the file, of course, messy things will be output. This is also a mistake that many beginners have ever made. Therefore, it is a good habit to type the parentheses at one time and write the file function open and close together.

  5. Check whether your file opening method is wrong, if it is a text file, you can do not add b to the opening method, if it is a binary file, you must add b . As follows

FILE *fp1,*fp2;
fp1=fopen("D:\\file\\ff.txt","r+");//"+"表示可读可写
fp2=fopen("D:\\file\\cc.dat","rb+");

Personal experience is limited, other students and big guys are welcome to supplement and learn from each other~

Guess you like

Origin blog.csdn.net/lidancsdn/article/details/106980259