C++_C\C++文件操作

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值