fopen ,cstdioFile,ifstream速率

// TODO: 在此添加控件通知处理程序代码
	
	CString lpszFilePath= _T("C:\\cn.txt");
		if (!PathFileExists(lpszFilePath))
			return;
		 

		__int64 nRows = 0, nRows1 = 0, nRows2=0, nRows3 = 0;
		FILE *fp;
		int nLen = 1024 * 128;
		char app1[1024 * 128] = { 0 };
		__int64 time_start1 = GetTickCount();
	//	m_timeCount.Begin();
		 
		
		errno_t _result =_wfopen_s(&fp, lpszFilePath, _T("r"));
		printf("%d\n", GetLastError());
			while (!feof(fp))
			{
				memset(app1, 0x00, nLen);
				fgets(app1, nLen, fp);
				nRows++;
			}
		 
	 
		printf("%d\n", GetLastError());
		fclose(fp);
		//printf("%s\n", app1);
		 //__int64 nSpan = m_timeCount.End();
		__int64 time_start2 = GetTickCount();
		



		CStdioFile file;
		CString strContent;
		__int64 time_start3 = GetTickCount();
	//	m_timeCount.Begin();
		if (file.Open(lpszFilePath, CFile::modeRead))
		{
			while (file.ReadString(strContent))
			{
				nRows1++;
			}
			file.Close();
		}
		//__int64 nSpan1 = m_timeCount.End();
		__int64 time_start4= GetTickCount();

		__int64 time_start5= GetTickCount();
	//	string lpszFilePath = "inputFileName";
		string input_file = "C:\\cn.txt";
		string query;
		ifstream in(input_file);
		while (getline(in, query)) {
			nRows2++;
			//cout << query << endl;
		}
		__int64 time_start6 = GetTickCount();


		__int64 time_start7 = GetTickCount();
		Read("C:\\1.txt");
		__int64 time_start8 = GetTickCount();

		CString str;
		str.Format(_T("File:%I64d(ms)-%I64d(rows)\r\nCStdioFile:%I64d(ms)-%I64d(rows)\r\nifstream:%I64d(ms)-%I64d(rows)\r\nReadFile:%I64d(ms)-%I64d(rows)"), 
			time_start2- time_start1, nRows, time_start4 - time_start3, nRows1, time_start6 - time_start5, nRows2,
			time_start8 - time_start7, nRows3);
		AfxMessageBox(str);
	  
	 
}
BOOL Read(char *filePath)
{
	HANDLE pFile;
	DWORD fileSize;
	char *buffer, *tmpBuf;
	DWORD dwBytesRead, dwBytesToRead, tmpLen;

	pFile = CreateFileA(filePath, GENERIC_READ,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,        //打开已存在的文件 
		FILE_ATTRIBUTE_NORMAL,
		NULL);

	if (pFile == INVALID_HANDLE_VALUE)
	{
		printf("open file error!\n");
		CloseHandle(pFile);
		return FALSE;
	}

	fileSize = GetFileSize(pFile, NULL);          //得到文件的大小

	buffer = (char *)malloc(fileSize);
	ZeroMemory(buffer, fileSize);
	dwBytesToRead = fileSize;
	dwBytesRead = 0;
	tmpBuf = buffer;
	 
	do {                                       //循环读文件,确保读出完整的文件    
		 
		ReadFile(pFile, tmpBuf, dwBytesToRead, &dwBytesRead, NULL);
		int x = GetLastError();
		if (dwBytesRead == 0)
			break;

		dwBytesToRead -= dwBytesRead;
		tmpBuf += dwBytesRead;

	} while (dwBytesToRead > 0);

	//  TODO 处理读到的数据 buffer
	 
	free(buffer);
	CloseHandle(pFile);

	return TRUE;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值