Problem with ifstream in loops

Hi guys,

Im writing a program that that will read a number of XML files, extract the information i need and write said info to a new txt file with the same name (but with a extension .txt).

To do this the code performs the following operations.

1. Reads a .txt file called "XMLFileNames" that contains all the xml file names I want to access and then store these in two vectors for later use.

2. Access one of the vectors and add ".gnd.xml" to the already stored string.

3. Access the other vector and add txt" to the already stored string.

4. Open each xml file in a loop and store the extracted information into a new .txt file.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    
    string name;
    vector<string> nameArray; // stores the name of each file being read
    vector<string> txtNameArray; // stores the name of the file that will be written to
    
    ifstream fileName; //file containing the name of all xml files to be opened
    ifstream xmlFile;
    ofstream txtFile;

    string txtPath = "C:\\Users\\Padraig\\Desktop\\College\\Masters\\Thesis\\Source Code\\FERET\\ground_truths\\txt\\";

//--------------------------------------------------------------------------------
// Open .txt file and confirm they can be opened

    fileName.open("XMLFileNames.txt");
   
    if(!fileName) { // file couldn't be opened
       cout << "Error:  file could not be opened" << endl;
    }
    
//--------------------------------------------------------------------------------
// Read each line of the file and save
//    it to arrays called nameArray and txtNameAray.
 

    while(fileName >> name){  
      nameArray.push_back(name);
      txtNameArray.push_back(name);
    }
    
    fileName.close();
    
//--------------------------------------------------------------------------------
// Give all the file names stored in nameArray an extension .gnd.xml 
// and all names in txtnameArray an extension of .txt

    int size = nameArray.size();
    string fullName;
       
    for(int i = 0; i < size; i++){
    
       string tmp =  nameArray.at(i) + ".gnd.xml";
       string temp = nameArray.at(i) + ".txt";
       nameArray.at(i) = tmp;
       txtNameArray.at(i) = temp;
//       cout << txtNameArray.at(i) << endl;
    }

//--------------------------------------------------------------------------------
// Open all the .xml files in a loop and save the info needed
// to a file
     
    string tmp, temp, sentence;  
    string oFace = "<face>";
    string cFace = "</face>";
    string info;
    
    for(int i = 0; i < size; i++){

       tmp = nameArray.at(i);
       temp = txtPath + txtNameArray.at(i);      

       xmlFile.open(tmp.c_str());
       txtFile.open(temp.c_str());
       
       if(!xmlFile) { // file couldn't be opened
          cout << "Error: file " << tmp << " could not be opened" << endl;
       }
       if(!txtFile) { // file couldn't be opened
          cout << "Error: file " << temp << " could not be opened" << endl;
       }
       
       bool search = false;
       
       while(xmlFile >> sentence){
          
          //if the line is correct right it to a file          
          if(search)
             txtFile << sentence << endl; 
            
          if(sentence.compare(oFace) == 0)
             search = true;
          else if(sentence.compare(cFace) == 0)
             search = false;
          
       }

       xmlFile.close();
       txtFile.close();
    }   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}


If I comment out the while loop from line 85 to 96 I can succesully access all the xml file and write to all the txt file.

The problem I am having is that the while loop works when its not in a for loop and I replace i with a number in tmp = nameArray.at(i); at line 70 and temp = txtPath + txtNameArray.at(i); at 71.

This works every time no matter what number i enter, but once I try and do it with the for loop it will work for the first number and never again,

The ouput is the message below repeated for every file except the first file.
Error: file 01203fb010_940128.gnd.xml could not be opened


Can anyone see the problem and suggest a solution?
closed account (DSLq5Di1)
I think you'll need to clear the stream state before reusing it,

1
2
3
4
5
xmlFile.close();
xmlFile.clear();

txtFile.close();
txtFile.clear();


That said, I don't see why it is necessary for you to declare xmlFile/txtFile at the top of main as you have.. instead I would recommend constructing the streams directly in your loop,

1
2
3
4
5
6
7
8
9
10
11
for(int i = 0; i < size; i++){

       tmp = nameArray.at(i);
       temp = txtPath + txtNameArray.at(i);

       ifstream xmlFile(tmp.c_str());
       ofstream txtFile(temp.c_str());
...

       xmlFile.close(); // will be closed by the destructor
       txtFile.close();


-edit-

The constructor also accepts strings, so you can get rid of the c_str() calls.
Last edited on
Hey Sloppy9,

Thanks for that. Program works and saves the information I need now.

Big help. Cheers.
Last edited on
Topic archived. No new replies allowed.