ifstream::tellg()
特定のファイルに対して-13を返しています。
基本的に、私はいくつかのソース コードを分析するユーティリティを作成しました。私はすべてのファイルをアルファベット順に開き、「Apple.cpp」から始めて完全に動作します..しかし、「Conversion.cpp」に到達すると、常に同じファイルで、1 行を正常に読み取った後、tellg() は -13 を返します。
問題のコードは次のとおりです。
for (int i = 0; i < files.size(); ++i) { /* For each .cpp and .h file */
TextIFile f(files[i]);
while (!f.AtEof()) // When it gets to conversion.cpp (not on the others)
// first is always successful, second always fails
lines.push_back(f.ReadLine());
コードAtEof
は次のとおりです。
bool AtEof() {
if (mFile.tellg() < 0)
FATAL(format("DEBUG - tellg(): %d") % mFile.tellg());
if (mFile.tellg() >= GetSize())
return true;
return false;
}
Conversion.cpp の最初の行を正常に読み取った後、常にクラッシュしDEBUG - tellg(): -13
ます。
これはクラス全体TextIFile
です(私が書いたもので、エラーがあるかもしれません):
class TextIFile
{
public:
TextIFile(const string& path) : mPath(path), mSize(0) {
mFile.open(path.c_str(), std::ios::in);
if (!mFile.is_open())
FATAL(format("Cannot open %s: %s") % path.c_str() % strerror(errno));
}
string GetPath() const { return mPath; }
size_t GetSize() { if (mSize) return mSize; const size_t current_position = mFile.tellg(); mFile.seekg(0, std::ios::end); mSize = mFile.tellg(); mFile.seekg(current_position); return mSize; }
bool AtEof() {
if (mFile.tellg() < 0)
FATAL(format("DEBUG - tellg(): %d") % mFile.tellg());
if (mFile.tellg() >= GetSize())
return true;
return false;
}
string ReadLine() {
string ret;
getline(mFile, ret);
CheckErrors();
return ret;
}
string ReadWhole() {
string ret((std::istreambuf_iterator<char>(mFile)), std::istreambuf_iterator<char>());
CheckErrors();
return ret;
}
private:
void CheckErrors() {
if (!mFile.good())
FATAL(format("An error has occured while performing an I/O operation on %s") % mPath);
}
const string mPath;
ifstream mFile;
size_t mSize;
};
プラットフォームは、Visual Studio、32 ビット、Windows です。
編集: Linuxで動作します。
編集:原因が見つかりました:行末。Conversion と Guid とその他の両方に \r\n の代わりに \n がありました。代わりに \r\n で保存しましたが、うまくいきました。それでも、これは起こらないはずですよね?