Common operations of MFC processing files

MFC judges whether a file or folder exists function

CString Path;
BOOL rec = PathFileExists(Path);
if(rec)
{
    
    
    //存在
}else{
    
    
    // 不存在
}

Create a file

CString filename=_T("D:/test.txt");
CFile file(filename,CFile::modeCreate);
file.Close();

write file

    CString filename =_T("D:/test.txt");
    CFile file(filename,CFile::modeWrite);
    char str[]="XXXXXX";
    file.Write(str,sizeof(str));
    file.Close();

read file

	CString filename=_T("D:/test.txt");
    CFile file(filename,CFile::modeRead);
    char *str=new char[file.GetLength()];
    file.Read(str,file.GetLength*sizeof(char));
    file.Close();

C language new file (txt)

// path = "D:\\log.txt"
FILE * r=fopen(path,"w");
fclose(r);

c language read file

char str[256] = {
    
    0};
FILE * r=fopen(path,"r");
fscanf(r,"%s",str);
fclose(r);

c language write file

char str[256] = {
    
    0};
FILE * r=fopen(path,"w");// a+  在末尾追加内容
fprintf(r,"%s","D:\\");
fclose(r);

Guess you like

Origin blog.csdn.net/qq_44391957/article/details/124579602