请教WriteFile函数如何使用,能否列举个例子,谢谢
解决方案 »
- 谁有PUDN的帐号啊?
- 服务端用TCP监听,客户端能否用UDP与服务端通信?
- 求助,多边形求交问题
- 关于在VC++的Dll中调用Delphi的Dll,且返回及传输的都是对象
- 有关MSCOMM32.ocx串口控件的问题??
- 求“购”一个最简单的C/S网络程序(收到发分,200分)
- 100分图形格式转换问题
- 看过VC知识库在线杂志15期中<在MFC中显示JPG/GIF图像的朋友请进来看看,谢了
- 急救~~~~~~~~~~~~~~要分就进~~~~~~~~~会的不难~~~~~~不会就难上天~~~~`
- Winsock控件中调用GetData读取数据
- [第4期]给基础版前几名的少许奖励以表彰
- oledb打开数据表的问题,初学者求助
HANDLE hAppend;
DWORD dwBytesRead, dwBytesWritten, dwPos;
char buff[4096];
// Open the existing file.
hFile = CreateFile("ONE.TXT", // open ONE.TXT
GENERIC_READ, // open for reading
0, // do not share
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template if (hFile == INVALID_HANDLE_VALUE)
{
ErrorHandler("Could not open ONE."); // process error
}
// Open the existing file, or if the file does not exist,
// create a new file.
hAppend = CreateFile("TWO.TXT", // open TWO.TXT
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // no security
OPEN_ALWAYS, // open or create
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template if (hAppend == INVALID_HANDLE_VALUE)
{
ErrorHandler("Could not open TWO."); // process error
}
// Append the first file to the end of the second file.
// Lock the second file to prevent another process from
// accessing it while writing to it. Unlock the
// file when writing is finished.
do
{
if (ReadFile(hFile, buff, 4096, &dwBytesRead, NULL))
{
dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
LockFile(hAppend, dwPos, 0, dwPos + dwBytesRead, 0);
WriteFile(hAppend, buff, dwBytesRead,
&dwBytesWritten, NULL);
UnlockFile(hAppend, dwPos, 0, dwPos + dwBytesRead, 0);
}
} while (dwBytesRead == 4096);
// Close both files.
CloseHandle(hFile);
CloseHandle(hAppend);
木有msdn的日子没法过~~
HANDLE hFile,
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped
); 函数声明#include <windows.h>
#include <stdio.h>#define BUFSIZE 4096int main()
{
HANDLE hFile;
HANDLE hTempFile;
DWORD dwBytesRead, dwBytesWritten, dwBufSize=BUFSIZE;
char szTempName[MAX_PATH];
char buffer[BUFSIZE];
char lpPathBuffer[BUFSIZE];
// Open the existing file.
hFile = CreateFile("original.txt", // file name
GENERIC_READ, // open for reading
0, // do not share
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no template
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file.");
return 0;
}
// Get the temp path GetTempPath(dwBufSize, // length of the buffer
lpPathBuffer); // buffer for path // Create a temporary file.
GetTempFileName(lpPathBuffer, // directory for temp files
"NEW", // temp file name prefix
0, // create unique name
szTempName); // buffer for name hTempFile = CreateFile((LPTSTR) szTempName, // file name
GENERIC_READ | GENERIC_WRITE, // open for read/write
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing file
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no template if (hTempFile == INVALID_HANDLE_VALUE)
{
printf("Could not create temporary file.");
return 0;
}
// Read 4K blocks to the buffer.
// Change all characters in the buffer to upper case.
// Write the buffer to the temporary file.
do
{
if (ReadFile(hFile, buffer, 4096,
&dwBytesRead, NULL))
{
CharUpperBuff(buffer, dwBytesRead);
WriteFile(hTempFile, buffer, dwBytesRead,
&dwBytesWritten, NULL);
}
} while (dwBytesRead == BUFSIZE);
// Close both files.
CloseHandle(hFile);
CloseHandle(hTempFile);
// Move the temporary file to the new text file.
if (!MoveFileEx(szTempName, "allcaps.txt", MOVEFILE_REPLACE_EXISTING))
{
printf("Could not move temp file.");
return 0;
}
}
典型的文件操作都有
hFile=CreateFile("5.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dwWrites;
WriteFile(hFile,"http://www.sunxin.org",strlen("http://www.sunxin.org"),
&dwWrites,NULL);
CloseHandle(hFile);*/