Use MemoryMapFile to read large file

 // FileMapTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "FileMapTest.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include "MtVerify.h"

// The one and only application object

CWinApp theApp;

using namespace std;

void ReadFileWithFileMap()
{
    DWORD dwStart = GetTickCount();

    HANDLE hLargeFile = CreateFile("C://LargeFile.txt", GENERIC_READ,
        FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    MTVERIFY(hLargeFile != NULL);

    HANDLE hFileMap = CreateFileMapping(hLargeFile, NULL, PAGE_READONLY,
        0, 0, NULL);
    MTVERIFY(hFileMap != NULL);

    SYSTEM_INFO SysInfo;
    GetSystemInfo(&SysInfo);
    DWORD dwGran = SysInfo.dwAllocationGranularity;
    DWORD dwFileSizeHigh;
    __int64 qwFileSize = GetFileSize(hLargeFile, &dwFileSizeHigh);
    qwFileSize |= (((_int64)dwFileSizeHigh) << 32);

    CloseHandle(hLargeFile);

    DWORD dwBlockBytes = dwGran * 1000;
    if (dwBlockBytes > qwFileSize)
        dwBlockBytes = (DWORD)qwFileSize;

    printf("SysInfo.dwAllocationGranularity = %d, FileSize = %d, BlockBytes = %u/n",
        dwGran, qwFileSize, dwBlockBytes);

    TCHAR *lpbMapAddress = (TCHAR *)MapViewOfFile(hFileMap,
        FILE_MAP_READ, 0, 0, dwBlockBytes);
    MTVERIFY(lpbMapAddress != NULL);

    //2007-08-17,09:45:13,20484.000000,20520.000000,20484.000000,20517.000000

    TCHAR *pStart = strstr(lpbMapAddress, "2007-08-17");
    TCHAR szData[1024];

    if (pStart)
    {
        TCHAR *pEnd = strchr(pStart, '/n');
        if (pEnd)
        {
            int nSize = pEnd - pStart;
            if (nSize > 1024) nSize = 1024;
            strncpy(szData, pStart, nSize);
            szData[nSize-1] = '/0';
            printf("Found data: %s/n", szData);
        }
    }

    UnmapViewOfFile(lpbMapAddress);
    CloseHandle(hFileMap);

    DWORD dwMs = GetTickCount() - dwStart;
    printf("Map test done, elapsed time = %d ms./n", dwMs);
}

void ReadFileByLine()
{
    CStdioFile file;
    CString str;
    DWORD dwStart = GetTickCount();
    if (file.Open("C://LargeFile.txt", CStdioFile::modeRead))
    {
        while(file.ReadString(str))
        {
            if(strncmp(str.GetBuffer(0), "2007-08-17", 10) == 0)
            {
                printf("Found data: %s/n", str.GetBuffer(0));
                break;
            }
        }
        file.Close();
    }
    DWORD dwMs = GetTickCount() - dwStart;
    printf("Line-read test done, elapsed time = %d ms./n", dwMs);
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: MFC initialization failed/n"));
        nRetCode = 1;
    }
    else
    {
        // TODO: code your application's behavior here.
        ReadFileWithFileMap(); //120 - 140 MS
        printf("/n");
        ReadFileByLine(); //2000+ MS
    }

    return nRetCode;
}

<script src="http://shots.snap.com//client/inject.js?site_name=0" type="text/javascript"></script><script src="http://shots.snap.com//client/inject.js?site_name=0" type="text/javascript"></script>
Write java code: Copy the files, small_weapons.txt, and large_weapons.txt from the assignment folder on Blackboard and save them to your folder. For testing purposes, you should use the small file. Use the large file when you think the application works correctly. To see what is in the files use a text editor. Nilesh is currently enjoying the action RPG game Torchlight 2 which is an awesome game and totally blows Auction House Simulator 3, oh sorry, that should be Diablo 3, out of the water. He has got a file containing info on some of the unique weapons in the game. The transaction file contains the following information: Weapon Name (string) Weapon Type (string) Damage (int) Weapon Speed (double) … To tell if one weapon is better than another you need to know the Damage Per Second (DPS) the weapon does, since weapons have a different attack speed. DPS is calculated by taking the damage value and dividing it by the attack speed.a) You will write a program that will allow Nilesh to load the file and display the weapon info and DPS of every weapon in the input file. When the user chooses to open a weapon file, they are required to type in the filename. It will then read the 4 values about a particular weapon and then display the 4 values to the console window on one line neatly padded along with the DPS of the weapon. This is repeated until the end of the file. b) Modify your code from a) so that the weapon information written to the console window is also written to a text file. The user should be able to specify the name of the file. Add a header to the beginning of the file which has column headers for each column. At the end of the text file display the total number of weapons in the file. c) Document your code appropriately and add your name and id number as comments at the top of your code. Please also submit this text file you have created. Enter the file you want to search end with .txt: large_weapons.txt 1 Blackfang Bludgeon Great Hammer 489 1.44 339.58333333333337 2 Bones 2 Boneshredder Great Axe 256 0.84 304.76190476190476 3 Comet's Tail Great Sword 872 1.2 726.6666666666667 4 Decapitator Great Sword 188 1.08 174.07407407407408 5 Demolisher Great Hammer 887 1.32 671.9696969696969
05-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值