fopen ,cstdioFile,ifstream速率

回眸只為那壹抹淺笑 提交于 2019-12-26 04:42:42
// 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;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!