下载+CreateFile+WriteFile+GetFileSize

int   CUpdate::Download(const char* src, const char* des)
{
	
	HINTERNET internet = InternetOpen("HTTP Downloader", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL) ;
	if (internet == INVALID_HANDLE_VALUE || NULL == internet)
	{
		return -1;
	}

	HINTERNET file_handle = InternetOpenUrl(internet, src, NULL, 0, INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE/*INTERNET_FLAG_DONT_CACHE*/, 0) ;
	if (NULL == file_handle || INVALID_HANDLE_VALUE == file_handle)
	{
		InternetCloseHandle(internet) ;
		return -2;
	}

	ULONG uk = 0;
	DWORD dwStatusCode = 0;
	DWORD dwStatusSize = sizeof(dwStatusCode);
	if (FALSE == ::HttpQueryInfo(file_handle,
		HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
		&dwStatusCode,
		&dwStatusSize,
		&uk))  //获取返回状态码
	{
		InternetCloseHandle(internet) ;
		InternetCloseHandle(file_handle);
		return -3;
	}
	//判断状态码是不是 200
	if (HTTP_STATUS_OK != dwStatusCode)
	{
		InternetCloseHandle(internet) ;
		InternetCloseHandle(file_handle);
		return -4;
	}


	HANDLE hFile = CreateFile(des, GENERIC_ALL, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if(hFile == INVALID_HANDLE_VALUE)
	{

		InternetCloseHandle(internet) ;
		InternetCloseHandle(file_handle);
		return -5;
	}


	DWORD dwReadDataLength = NULL;
	BOOL bRet = TRUE;
	// 下面将读取的内容写入本地文件中
	//std::string::size_type nPos = strTmpPath.rfind("\\") ;
	//std::string strFileName = strTmpPath.substr(nPos + 1) 


	DWORD dwMaxDataLength = 1024;
	char* pBuffer = new  char[dwMaxDataLength + 1];
	do 
	{
		DWORD dwWritenSize = 0;
		ZeroMemory(pBuffer,dwMaxDataLength);
		bRet = InternetReadFile(file_handle,pBuffer,dwMaxDataLength,&dwReadDataLength);
		if(bRet ==  TRUE)
		{
			pBuffer[dwReadDataLength] = 0 ;
			WriteFile(hFile, pBuffer, dwReadDataLength, &dwWritenSize, NULL);
		}
	

	} while (NULL != dwReadDataLength);

	if(pBuffer)
		delete[] pBuffer;

	InternetCloseHandle(internet) ;
	InternetCloseHandle(file_handle);
	CloseHandle(hFile);



	return 0;
}
DWORD CUpdate::GetFileSize(const char* file)
{
	DWORD dwLen = 0;
	HANDLE hFile = CreateFile(file, GENERIC_ALL, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		dwLen = ::GetFileSize(hFile, NULL);
		CloseHandle(hFile);
	}

	return dwLen;


}


感谢您的提问。根据您的问题,我了解到您需要进行一个文件操作的实验,并且需要使用到 CreateFile()、ReadFile()、WriteFile() 和 CloseHandle() 等函数。您的具体问题是如何读取一个文本文件,并将其内容写入另一个文本文件。下面是一个基本的 C++ 代码示例,可以实现您的需求: ```cpp #include <windows.h> #include <iostream> using namespace std; int main() { // 打开原始文件 HANDLE hFile = CreateFile("input.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { cout << "Failed to open input.txt!" << endl; return 1; } // 获取文件大小 DWORD dwFileSize = GetFileSize(hFile, NULL); // 读取原始文件内容 char* pBuffer = new char[dwFileSize]; DWORD dwBytesRead = 0; if (!ReadFile(hFile, pBuffer, dwFileSize, &dwBytesRead, NULL)) { cout << "Failed to read input.txt!" << endl; CloseHandle(hFile); delete[] pBuffer; return 1; } // 关闭原始文件 CloseHandle(hFile); // 打开目标文件 hFile = CreateFile("output.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { cout << "Failed to create output.txt!" << endl; delete[] pBuffer; return 1; } // 写入目标文件内容 DWORD dwBytesWritten = 0; if (!WriteFile(hFile, pBuffer, dwBytesRead, &dwBytesWritten, NULL)) { cout << "Failed to write output.txt!" << endl; } // 关闭目标文件 CloseHandle(hFile); // 释放缓冲区 delete[] pBuffer; // 检查两个文件内容是否一致 system("fc input.txt output.txt"); return 0; } ``` 在这个例子中,我们首先通过 CreateFile() 函数打开原始文件 "input.txt",并通过 ReadFile() 函数读取文件内容。然后,我们通过 CreateFile() 函数创建目标文件 "output.txt",并通过 WriteFile() 函数将读取到的文件内容写入到目标文件中。最后,我们使用 system("fc input.txt output.txt") 命令来检查两个文件的内容是否一致。 需要注意的是,以上代码只是一个基本的示例,还有许多细节需要处理。例如,我们需要在使用完文件句柄后使用 CloseHandle() 函数关闭文件,需要在使用完缓冲区后使用 delete[] 操作符释放内存等等。此外,如果您需要进行更高级的文件操作,例如读取二进制文件、使用缓存等等,还需要进一步学习和了解相关知识。 希望以上内容能够帮助到您,如果您有任何问题,欢迎随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值