質問

ifstream::tellg()特定のファイルのために-13を返してます。

基本的に、私はいくつかのソースコードを解析して、ユーティリティを書きました。 ()が復帰を-13 tellg首尾1行を読んだ後、私は「Apple.cpp」で始まり、それが完璧に動作し、アルファベット順にすべてのファイルを開く..しかし、それは「Conversion.cpp」に到達したときに、常に同じファイルに

問題のコードは次のとおりです。

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上での作品

の編集:の私は原因が見つかりました:行末。変換およびGUIDと他の人の両方がn個の代わりに\ rを\ nは\ていました。私の代わりに、\ rを\ nはそれらを保存し、それが働きました。それでも、これが起こることになっていませんが、それですか?

役に立ちましたか?

解決

これはConversion.cppにあります正確に何を知らなくても推測することは困難です。しかし、ストリームの位置と<を使用すると、標準で定義されていません。あなたはそれをフォーマットする前に、正しい整数型への明示的なキャストを検討する必要があります。私は、書式設定FATALformat()%演算子がオーバーロードされている方法を実行したりすることを期待か分かりません。ストリームの位置は、ファイルがバイナリモードで開かれていない場合は、確かに、整数にpredicatableの方法ではないマップする必要はありません。

あなたはAtEof()のための代替実装を検討する必要があります。

:のようなものを言います
bool AtEof()
{
    return mFile.peek() == ifstream::traits_type::eof();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top