一次读取一个文件块时出现逐一错误
Off-by-one errors when reading a file one chunk at a time
由于实现定义的问题,ifstream::readsome
对于读取文件块来说是出了名的糟糕。在我的例子中,MSVC returns 0 在新打开的文件上。
我查看了他们的实现并意识到他们只是在后台调用 ifstream::read
:
MSVC 实现
streamsize __CLR_OR_THIS_CALL readsome(_Elem* _Str,
streamsize _Count) { // read up to _Count characters into buffer, without blocking
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);
streamsize _Num;
if (!_Ok) {
_State |= ios_base::failbit; // no buffer, fail
} else if ((_Num = _Myios::rdbuf()->in_avail()) < 0) {
_State |= ios_base::eofbit; // no characters available
} else if (0 < _Count && 0 < _Num) { // read available
read(_Str, _Num < _Count ? _Num : _Count);
}
_Myios::setstate(_State);
return gcount();
}
所以我实现了自己的调用 ifstream::read
:
我的实现
std::optional<std::string> ReadSomeStringFromFile(std::ifstream& ifs, std::streampos pos, std::streamsize count) noexcept {
if(ifs && ifs.is_open()) {
auto result = std::string(count, '[=11=]');
ifs.seekg(pos);
ifs.read(result.data(), count);
if(ifs.gcount()) {
return result;
}
}
return {};
}
用法:
std::streampos pos{0};
std::streamsize count{10};
std::ifstream ifs{g_options_filepath};
{
auto stream = FileUtils::ReadSomeStringFromFile(ifs, pos, count);
while(stream.has_value()) {
DebuggerPrintf(stream.value().c_str());
pos += count;
stream = FileUtils::ReadSomeStringFromFile(ifs, pos, count);
}
}
这适用于二进制文件(我有一个单独的函数),但对于字符串版本 我需要保留换行符 会产生一个 off-by-如果块包含换行符,则出错。这会导致块中的最后一个字符被复制为下一个中的第一个字符:
预期输出
difficulty=Normal
controlpref=Mouse
sound=5
music=5
cameraShakeStrength=1.000000
实际产量
difficulty=Normal
coontrolpref=Mouse
souund=5
musiic=5
camerraShakeStrength=1.000000
使用格式化 ifstream::get
默认情况下使用换行符作为分隔符并完全跳过它(同样,需要保留换行符)并导致交错输出和丢弃字符:
difficult=Normalontrolpre=Mouseund=5ic=5raShakeStength=1.00000
问题
有没有办法尝试在格式化数据上使用未格式化的输入函数,或者我不应该尝试使用文本数据?
我不经常使用 get
,所以我忘记了它的存在。使用 this answer as a guide 我想出了一个解决方案:
(我仔细检查过,另一个使用 FormattedInput
作为 (ifs >> std::noskipws >> ch)
的答案给出了相同的结果,即使 get
规范说它将它视为 UnformattedInput
)
[[nodiscard]] std::optional<std::string> ReadSomeStringBufferFromFile(std::ifstream& ifs, std::streampos pos, std::streamsize count /*= 0u*/) noexcept {
if(!(ifs && ifs.is_open())) {
return {};
}
ifs.seekg(pos, std::ios::beg);
//Would like to early-out here,
//but MSVC ifstream::seekg doesn't set the fail bit,
//so can't early-out until the get call.
char ch{};
std::string result{};
result.reserve(count);
bool readsome{false}; //If nothing read, make std::optional::has_value false.
while(ifs && ifs.get(ch) && count > 0) {
result.append(1, ch);
--count;
readsome |= true;
}
return readsome ? std::make_optional(result) : std::nullopt;
ifstream::readsome
对于读取文件块来说是出了名的糟糕。在我的例子中,MSVC returns 0 在新打开的文件上。
我查看了他们的实现并意识到他们只是在后台调用 ifstream::read
:
MSVC 实现
streamsize __CLR_OR_THIS_CALL readsome(_Elem* _Str,
streamsize _Count) { // read up to _Count characters into buffer, without blocking
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);
streamsize _Num;
if (!_Ok) {
_State |= ios_base::failbit; // no buffer, fail
} else if ((_Num = _Myios::rdbuf()->in_avail()) < 0) {
_State |= ios_base::eofbit; // no characters available
} else if (0 < _Count && 0 < _Num) { // read available
read(_Str, _Num < _Count ? _Num : _Count);
}
_Myios::setstate(_State);
return gcount();
}
所以我实现了自己的调用 ifstream::read
:
我的实现
std::optional<std::string> ReadSomeStringFromFile(std::ifstream& ifs, std::streampos pos, std::streamsize count) noexcept {
if(ifs && ifs.is_open()) {
auto result = std::string(count, '[=11=]');
ifs.seekg(pos);
ifs.read(result.data(), count);
if(ifs.gcount()) {
return result;
}
}
return {};
}
用法:
std::streampos pos{0};
std::streamsize count{10};
std::ifstream ifs{g_options_filepath};
{
auto stream = FileUtils::ReadSomeStringFromFile(ifs, pos, count);
while(stream.has_value()) {
DebuggerPrintf(stream.value().c_str());
pos += count;
stream = FileUtils::ReadSomeStringFromFile(ifs, pos, count);
}
}
这适用于二进制文件(我有一个单独的函数),但对于字符串版本 我需要保留换行符 会产生一个 off-by-如果块包含换行符,则出错。这会导致块中的最后一个字符被复制为下一个中的第一个字符:
预期输出
difficulty=Normal
controlpref=Mouse
sound=5
music=5
cameraShakeStrength=1.000000
实际产量
difficulty=Normal
coontrolpref=Mouse
souund=5
musiic=5
camerraShakeStrength=1.000000
使用格式化 ifstream::get
默认情况下使用换行符作为分隔符并完全跳过它(同样,需要保留换行符)并导致交错输出和丢弃字符:
difficult=Normalontrolpre=Mouseund=5ic=5raShakeStength=1.00000
问题
有没有办法尝试在格式化数据上使用未格式化的输入函数,或者我不应该尝试使用文本数据?
我不经常使用 get
,所以我忘记了它的存在。使用 this answer as a guide 我想出了一个解决方案:
(我仔细检查过,另一个使用 FormattedInput
作为 (ifs >> std::noskipws >> ch)
的答案给出了相同的结果,即使 get
规范说它将它视为 UnformattedInput
)
[[nodiscard]] std::optional<std::string> ReadSomeStringBufferFromFile(std::ifstream& ifs, std::streampos pos, std::streamsize count /*= 0u*/) noexcept {
if(!(ifs && ifs.is_open())) {
return {};
}
ifs.seekg(pos, std::ios::beg);
//Would like to early-out here,
//but MSVC ifstream::seekg doesn't set the fail bit,
//so can't early-out until the get call.
char ch{};
std::string result{};
result.reserve(count);
bool readsome{false}; //If nothing read, make std::optional::has_value false.
while(ifs && ifs.get(ch) && count > 0) {
result.append(1, ch);
--count;
readsome |= true;
}
return readsome ? std::make_optional(result) : std::nullopt;