opening a text file

hi,

i've done everything according to this : http://www.cplusplus.com/doc/tutorial/files/

but i have a problem, if i open only one time my file, everything is fine, but the second time, it bugs

it won't enter my while(!myParaFile.eof){} or while(!myParaFile.good){}

here is my code :

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
void convertFileToMat(char *name, int ***mat)
   {
   //la matrice est conforme a la page 3 
   
   //parcourir tout le fichier afin de definir le nombre de ligne
  int counter = 0; 
  string line;
  ifstream myParaFile (name);//creation et ouverture en meme temps
  if (!myParaFile.is_open())
     {
     //impossible d'ouvrir
     }
  else
     {
       while ( !myParaFile.eof() )
          {
          getline (myParaFile,line);
          cout <<line<< endl;
          counter++;
          }
      myParaFile.close();
     } 
   cout << "\n\ntnere is "<<counter<<" lines"<<"\ninitializing matrix \n\n";
   
   //creation de la matrice
   
   //remplir la matrice par algo de cassage
   myParaFile.open(name); //ouiverture de l'objet

   cout << "opening file\n";
   //here tellg() is returning -1 (weird)
   myParaFile.seekg(0);
   cout << "position = "<<myParaFile.tellg()<<"\n";
   //here tellg() is still returning -1
   if (!myParaFile.is_open())
     {
     //ouverture impossble
     }
   else
     {
       while ( !myParaFile.eof() )
          {
          getline (myParaFile,line);
          cout <<line<< endl;
          //algo de cassage ici
          counter++;
          }  
      myParaFile.close();
     } 

     
}


any idea?

thanks :)
You might put a try/catch block around the second call to open() to see the error its throwing.
Topic archived. No new replies allowed.