C++

mac2025-05-17  20

目录

CC++ IOFSTREAMWin32 APICFileCStdioFile

C

FILE *pFile=fopen("1.txt","w"); fwrite("hello world",strlen("hello world"),1,pFile); fflush(pFile); fclose(pFile); FILE *pFile=fopen("1.txt","r"); char *buff; fseek(pFile,0,SEEK_END); int length=ftell(pFile); fseek(pFile,0,SEEK_SET); buff=new char[length+1]; fread(buff,1,length,pFile); buff[length]=0; MessageBoxA(NULL,buff,NULL,MB_OK); fclose(pFile);

C++ IOFSTREAM

#include <fstream> void Cfile11Dlg::OnBnClickedW() { std::ofstream ofs("2.txt"); ofs.write("hello world",strlen("hello world")); ofs.close(); } void Cfile11Dlg::OnBnClickedR() { std::ifstream ifs("2.txt"); char ch[100]=""; ifs.read(ch,100); ifs.close(); MessageBoxA(NULL,ch,NULL,MB_OK); }

Win32 API

void Cfile11Dlg::OnBnClickedW() { HANDLE handle; handle=CreateFile(TEXT("3.txt"),GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL); WriteFile(handle,"hello world",strlen("hello world"),NULL,NULL); CloseHandle(handle); } void Cfile11Dlg::OnBnClickedR() { HANDLE handle; handle=CreateFile(TEXT("3.txt"),GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); char ch[100]=""; DWORD dw; ReadFile(handle,ch,100,&dw,NULL); CloseHandle(handle); MessageBoxA(NULL,ch,NULL,MB_OK); }

CFile

void Cfile11Dlg::OnBnClickedW() { CFile file(TEXT("4.txt"),CFile::modeCreate|CFile::modeWrite); file.Write("hello world",strlen("hello world")); file.Close(); } void Cfile11Dlg::OnBnClickedR() { CFile file(TEXT("4.txt"),CFile::modeRead); DWORD length=file.GetLength(); char* ch; ch=new char[length+1]; ch[length]=0; file.Read(ch,length); file.Close(); MessageBoxA(NULL,ch,NULL,MB_OK); }

CStdioFile

MFC文件操作之CStdioFile

最新回复(0)