VC如何用CreateFile在D:\下创建一个名为“123.txt”的文本文档,并写入指定数据?

如题

#include <windows.h>

HANDLE hFile;
DWORD szBuffer[4];
const char szText[] = "QQ:610847323";
char Length;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nShowCmd)
{
hFile=CreateFile("D:\\123.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,
OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);//先创建一个空的文件
Length=lstrlen(szText);//计算自己要写入的长度
WriteFile(hFile,szText,Length,szBuffer,NULL);//写入刚才创建的空文件中
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-01-31
这个是我程序里的一段函数

因为你的分低懒得去剪裁了

void BackUp::restore(CString name,bool nFlag)
{
CFile file1,file2;
long flength;
char *buff;
CString f1,f2;
if(nFlag)
{
f1=name;
f2=_T("data.mdb");
}
else
{
f1=_T("data.mdb");
f2=name;
}
if(file1.Open(f1,CFile::modeRead))
{
flength=file1.GetLength();
buff=new char [flength];
file1.Read(buff,flength);
if(file2.Open(f2,CFile::modeWrite|CFile::modeCreate))
{
file2.Write(buff,flength);
if(nFlag)
AfxMessageBox("还原成功");
else
AfxMessageBox("备份成功");
}
else
AfxMessageBox("打开文件异常");
delete buff;
}
else
AfxMessageBox("打开文件异常");
}

name 表示文件名 nFlag表示是备份还是还原, 具体过程就是读一个文件,写入另一个文件
第2个回答  2010-01-31
CreateFile()函数只能创建文件,要写入文件,还要用到另一个函数WriteFile()
关于这两个函数的详细用法,你可以参考微软MSDN文档。

http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa365747(VS.85).aspx