IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

Problèmes lors de la lecture d'un fichier


Sujet :

C++

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 5
    Points : 2
    Points
    2
    Par défaut Problèmes lors de la lecture d'un fichier
    Bonjour,

    Voila j'ai un énorme (enfin tou tdu moins déroutant) problème sur un truc aussi idiot que la lecture d'un fichier

    Le fichier "essai" que je veux lire contient 10 fois cette même ligne :
    "abcdefghijklmnopqrstuvwxyz"

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    #include <iostream>
    #include <fstream>
    using namespace std;
     
    int main () 
    {
      ifstream inVF("essai", ios::in);
      inVF.seekg(0, ios::beg );
      long position = 0;
      while(!inVF.eof())
      {
        string chaineVF;
        position = inVF.tellg();
        getline(inVF, chaineVF);
        cout<<"## chaineVF : "<<chaineVF<<endl;
        cout<<"## position  : "<<position<<endl;
      }
      inVF.close();
      return 0;
    }
    sortie console :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    ## chaineVF : abcdefghijklmnopqrstuvwxyz
    ## position  : 0
    ## chaineVF : ijklmnopqrstuvwxyz
    ## position  : 36
    ## chaineVF : hijklmnopqrstuvwxyz
    ## position  : 63
    ## chaineVF : ghijklmnopqrstuvwxyz
    ## position  : 90
    ## chaineVF : fghijklmnopqrstuvwxyz
    ## position  : 117
    ## chaineVF : efghijklmnopqrstuvwxyz
    ## position  : 144
    ## chaineVF : defghijklmnopqrstuvwxyz
    ## position  : 171
    ## chaineVF : cdefghijklmnopqrstuvwxyz
    ## position  : 198
    ## chaineVF : bcdefghijklmnopqrstuvwxyz
    ## position  : 225
    ## chaineVF : abcdefghijklmnopqrstuvwxyz
    ## position  : 252
    Je soupconne trés fortement le tellg qui vient tout foutre en l'air, car quand je ne l'utilise pas tout ce passe bien, et franchement je vois pas pourquoi l'utilisation de tellg viendrait autant perturbé le getline, je suis perdu ...

    Alors si quelqu'un peut m'expliquer clairement d'où vient le problème ca serait sympa, merci

  2. #2
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    En fuinant un peu partout je suis tombé su run sujet interessant a propos d'un bug #13333 de MINGW(gcc), avec le format du fichier, qui était au format DOS, je viens d'essayer au format UNIX et ca passe, j'aurais perdu 3h à cause de ce foutu bug

  3. #3
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 518
    Points
    41 518
    Par défaut
    Je me disais bien qu'il n'était pas forcément prudent d'utiliser une fonction de positionnement en mode texte...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  4. #4
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    Ah exact en mode ios::binary, ca marche dans les deux formats

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    Arg, voila j'ai un nouvo probleme, j'ai pas voulu refaire un post, donc je post ici :

    le fichier "essai" contient :
    code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    #include <fstream>
    #include <string> 
    #include <iostream>
    using namespace std;
     
    int main( int argc, char* argv[] )
    {
      ifstream inputVF( "essai", ios::binary|ios::in );
      if(inputVF)
      {
        string chaine1;
        string chaine2;
     
        getline(inputVF, chaine1);
        cout<<"chaine 1 : "<<chaine1<<endl;
     
        getline(inputVF, chaine2);
        cout<<"chaine 2: "<<chaine2<<endl;
     
        cout << chaine1 << " @ " << chaine2 << endl;
     
        inputVF.close();
     
      }
      return 0;
    }
    sortie console :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    chaine 1 : 1111
    chaine 2: 0000
     @ 0000
    Où est passé chaine1 ? Mystère !
    Si j'enlève le mode binary ca marche, mais comme dit précédemment j'aurais des soucis avec le tellg si je l'enlève.
    Donc en quoi le mode binary dans cet exemple 'vide' la chaine1 ?

  6. #6
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    On m'a soufflé la réponse, le probleme vient du \r a la fin de chaine1, qui place le curseur au debut, ensuite on réécrit par dessus le " @ " et enfin chaine2.

    Donc c'est la derniere fois que j'utilise un fichier en mode DOS de ma vie !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. problème lors de la lecture d'un fichier
    Par salseropom dans le forum C++
    Réponses: 16
    Dernier message: 29/09/2011, 15h10
  2. Problème de split lors d'une lecture d'un fichier csv
    Par Mawinette dans le forum Entrée/Sortie
    Réponses: 7
    Dernier message: 25/03/2009, 11h48
  3. Problème lors de la lecture d'un fichier
    Par _SamSoft_ dans le forum C
    Réponses: 2
    Dernier message: 23/02/2008, 15h14
  4. Problème lors de la lecture d'un fichier xml avec XMLBeans
    Par oscar78 dans le forum Format d'échange (XML, JSON...)
    Réponses: 1
    Dernier message: 07/09/2007, 14h04
  5. Problème lors de la lecture d'un fichier avec Input...
    Par Kronoob dans le forum VB 6 et antérieur
    Réponses: 13
    Dernier message: 18/11/2005, 18h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo