Question

ifstream::tellg() retourne -13 pour un certain fichier.

En fait, j'ai écrit un utilitaire qui analyse du code source; J'ouvre tous les fichiers par ordre alphabétique, je commence par « Apple.cpp » et il fonctionne parfaitement .. Mais quand il arrive à « Conversion.cpp », toujours sur le même fichier, après avoir lu une ligne retourne tellg () avec succès -13.

Le code en question est:

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());

Le code pour AtEof est:

    bool AtEof() {
        if (mFile.tellg() < 0)
            FATAL(format("DEBUG - tellg(): %d") % mFile.tellg());
        if (mFile.tellg() >= GetSize())
            return true;

        return false;
    }

Après lit avec succès la première ligne de Conversion.cpp, il se bloque toujours avec DEBUG - tellg(): -13.

Ceci est toute la classe TextIFile (écrit par moi, l'erreur peut être là):

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;
};

Plate-forme est Visual Studio, 32 bits, Windows.

Edit:. Travaux sur Linux

Edit: Je trouve la cause: les fins de ligne. Les deux Conversion et Guid et d'autres avaient \ n au lieu de \ r \ n. Je les ai enregistrés avec \ r \ n au lieu et cela a fonctionné. Pourtant, ce n'est pas censé se produire est-il?

Était-ce utile?

La solution

Il est difficile de deviner sans savoir exactement ce qui est en Conversion.cpp. Cependant, l'utilisation < avec des positions de flux n'est pas défini par la norme. Vous voudrez peut-être envisager un casting explicite au type entier correct avant de la formater; Je ne sais pas quoi attendre la mise en forme et FATAL format() pour effectuer ou comment l'opérateur % est surchargé. positions flux ne doivent pas la carte d'une manière predicatable aux entiers, certainement pas si le fichier est pas ouvert en mode binaire.

Vous voudrez peut-être envisager une mise en œuvre alternative pour AtEof(). Dites quelque chose comme:

bool AtEof()
{
    return mFile.peek() == ifstream::traits_type::eof();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top