English 中文(简体)
在一系列未经签名的果园(反之亦然)中,读一个全色的卷宗的最快途径是什么?
原标题:What s the fastest way to read a file full of strings (separated by newlines) in an array of unsigned char arrays (and vice versa)?

当然,标题可能会误导。 各位都希望你尽快研究以下两条幻灯片,并就如何尽可能改善业绩,避免过于夸张。 守则只需要双赢。 不幸的是,STL集装箱现在不是一个选择。

改为......

bool TextFile::Read(const char *pFilePath)
{
    bool            bSuccess    = false;
    std::ifstream   oFile(pFilePath, std::ios_base::in);

    if(oFile.is_open())
    {
        std::string stLineNow;
        std::size_t siLineLength;

        if(this->pLines)
        {
            this->Clear();
        }

        this->stFilePath = pFilePath;

        oFile.seekg(0, std::ios::end);
        this->pLines = new unsigned char *[static_cast<unsigned int> (oFile.tellg())];
        oFile.seekg(0, std::ios::beg);

        for(this->ulLinesCount = 0; std::getline(oFile, stLineNow).good(); this->ulLinesCount++)
        {
            siLineLength = stLineNow.length() + 1;
            this->pLines[this->ulLinesCount] = new unsigned char[siLineLength];
            memcpy(this->pLines[this->ulLinesCount], stLineNow.c_str(), siLineLength);
        }

        bSuccess = true;

        oFile.close();
    }

    return bSuccess;
}

......

bool TextFile::Save(const char *pFilePath)
{
    bool bSuccess = false;

    if(this->pLines)
    {
        std::ofstream oFile(pFilePath ? pFilePath : this->stFilePath, std::ios_base::out);

        if(oFile.is_open())
        {
            for(unsigned long ulPosition = 0; ulPosition < this->GetCount(); ulPosition++)
            {
                oFile << this->Get(ulPosition) <<  
 ;
            }

            bSuccess = true;

            oFile.close();
        }
    }

    return bSuccess;
}

......,请以假装形式。

提前感谢!

最佳回答

......我要做的是:

  • Use memory-mapped files. That s THE fastest way of reading files;
  • Then memcpy() all the file s contents in a new buffer;
  • Then run through the buffer once and count the newlines;
  • Make an array of as many pointers as there are newlines;
  • Run through the buffer another time, and this time:
    • Replace the first byte of each newline with 0.
    • Place a pointer to the start of each line in the pointer array.

Voilà !

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签