c_cpp http://www.zhihu.com/question/27971703/answer/38857951

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp http://www.zhihu.com/question/27971703/answer/38857951相关的知识,希望对你有一定的参考价值。

#include <windows.h>
#include <stdio.h>

void main()
{
	HANDLE hFile;
	HANDLE hAppend;
	DWORD  dwBytesRead, dwBytesWritten, dwPos;
	BYTE   buff[4096];

	// Copy the existing file.

	BOOL bCopy = CopyFile(TEXT("1.txt"), TEXT("3.txt"), TRUE);
	if (!bCopy)
	{
		printf("Could not copy 1.txt to 3.txt.");
		return;
	}

	// Open the existing file.

	hFile = CreateFile(TEXT("2.txt"), // open 2.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)
	{
		printf("Could not open 2.txt.");
		return;
	}

	// Open the existing file, or if the file does not exist,
	// create a new file.

	hAppend = CreateFile(TEXT("3.txt"), // open 3.txt
		FILE_APPEND_DATA,         // open for writing
		FILE_SHARE_READ,          // allow multiple readers
		NULL,                     // no security
		OPEN_ALWAYS,              // open or create
		FILE_ATTRIBUTE_NORMAL,    // normal file
		NULL);                    // no attr. template

	if (hAppend == INVALID_HANDLE_VALUE)
	{
		printf("Could not open 3.txt.");
		return;
	}

	// 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 complete.

	while (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL)
		&& dwBytesRead > 0)
	{
		dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
		LockFile(hAppend, dwPos, 0, dwBytesRead, 0);
		WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NULL);
		UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);
	}

	// Close both files.

	CloseHandle(hFile);
	CloseHandle(hAppend);
}

以上是关于c_cpp http://www.zhihu.com/question/27971703/answer/38857951的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *

c_cpp 54.螺旋矩阵