读取无符号字符数组数组中充满字符串(以换行符分隔)的文件的最快方法是什么(反之亦然)?
好吧,也许标题有点误导。我希望您做的就是快速浏览以下两个片段,并给我一些关于如何在不使用太奇特的代码的情况下尽可能提高性能的提示。该代码仅需要在 win32 上运行。不幸的是,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) << '\n';
}
bSuccess = true;
oFile.close();
}
}
return bSuccess;
}
请原谅丑陋的格式。
提前致谢!
Well, maybe the title is misleading a bit. All I want you to do is to take a quick look at the following two snippets and to give me some tips on how to improve the performance as much as possible without getting too exotic code. The code is needed to function on win32 only. Unfortunately STL containers are not an option right now.
To read the file...
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;
}
... and to save it...
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) << '\n';
}
bSuccess = true;
oFile.close();
}
}
return bSuccess;
}
... and please excuse the ugly formatting.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯...我会做的是:
memcpy()
将文件的所有内容放入新的缓冲区中;0
。瞧!
Well... what I would do is:
memcpy()
all the file's contents in a new buffer;0
.Voilà !