Program Freezes on File Read

I have the following 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//Maplestory XML Parser
//Copyright (c) 2011 Cecil G. Bowen

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<conio.h>
#include<string>
using namespace std;

template<typename T>
int nthSubstr(int n, const basic_string<T>& s, const basic_string<T>& p, bool repeats = false)
{
   string::size_type i = s.find(p);
   string::size_type adv = (repeats) ? 1 : p.length( );

   int j;
   for (j = 1; j < n && i != basic_string<T>::npos; ++j)
      i = s.find(p, i+adv);

   if (j == n)
     return(i);
   else
     return(-1);
}

int main(int nNumberofArgs, char* pszArgs[])
{
    const int SIZE = 17704;
    int ans, tries = 0, good_line, i = 1;
    bool goodu = 0, goodp = 0; //Account Validating Booleans
    string filename, txtfile; //Name of Files
    string idParse = "<imgdir name=";
    string nameParse = "value=";
    string isName = "mapName";
    string line[SIZE]; //File Array *IMPORTANT*
    string id[SIZE]; //Obtained IDs Lists
    string name[SIZE]; //Obtained Names List

    //cout<<"\nXML Filename: ";
    //cin>>filename;
    //cin.clear();
    //cout<<"\nTXT Filename: ";
    //cin>>txtfile;
    //cin.clear();

    //Read Data from Text File
    ifstream myWZ;
    myWZ.open("map.img.xml");
    if (myWZ.is_open())
    {
        cout<<"opened xml";
        while (!myWZ.eof())
        {
            getline (myWZ,line[i]); //Stores Each File Line to the "line" Array
            cout<<"line: "<< i <<": " <<line[i] <<"\n";
            ++i;
        }
        myWZ.close();
    }
    else
    {
        system("cls"); //Clear Screen
        cout<<"File read error!  Please make sure the specified file exists!";
    }

    //Check File for ID
    int a, b; //Iterator Values for Storing IDs/Names
    for(int j=0; j<i; ++j)
    {
        if (line[j].find(idParse) != string::npos) //If id is found on current line...
        {
            if (line[j].find_first_of("0123456789") != string::npos)
            {
                int openQ = (line[j].find('"')) + 1; //Returns Position Inside First "
                int endQ = (line[j].rfind('"')) - 1; //Returns Position Inside End "
                id[a] = line[j].substr(openQ, (endQ-openQ)); //Add it to the ID List
                a++;
            }
        }
        else if (line[j].find(nameParse) != string::npos) //If name is found on current line...
        {
            if (line[j].find(isName) != string::npos) //If "mapName" is found on same line...
            {
                string quote = "\"";
                int openQ = nthSubstr(3, line[j],quote) + 1; //Find Position of 3rd "
                int endQ = nthSubstr(4, line[j],quote) - 1; //Find Position of 4th "
                name[b] = line[j].substr(openQ, (endQ-openQ)); //Add it to the Name List
                b++;
            }
        }
    }
    id[a] = "/";
    name[b] = "/";

    //Output Text to File
    ofstream myTXT;
    myTXT.open("txtID.txt",ios_base::app);  //Opens TXT File at End for Writing
    if (myTXT.is_open())
    {
        int x = 0;
        while (true)
        {
            if (id[x] == "/")
            {
                break;
            }
            myTXT <<id[x] <<"    " <<name[x] <<"\n";
            x++;
        }
        myTXT.close();
    }
    else
    {
        cout<<"\nfailed to write to file";
        return 0;
    }
}

It compiles fine, but when I run it, it freezes when it gets to line 17703 of my file: http://www.mediafire.com/?9vfr11lfa4laqbw
Seems simple enough, but I can't solve it.
Anyone care to help?
1
2
3
4
5
6
7
8
/*        while (!myWZ.eof())
        {
            getline (myWZ,line[i]); //Stores Each File Line to the "line" Array
            cout<<"line: "<< i <<": " <<line[i] <<"\n";
            ++i;
        }*/
for( int K=0; getline(myWZ, line[K]); ++K)
  cout<<"line: "<< K <<": " <<line[K] <<"\n";
I think that you will set the eof flag when you try to read eof. So it will be out of bounds (i=17704).
Also why i starts at 1 instead of 0?

int a, b; //Iterator Values for Storing IDs/Names uninitialized variables.
By the way, avoid system http://www.cplusplus.com/forum/articles/11153/ (also it is not portable, as well as conio)

You may want to review your style.
Try to declare the variables where you need them and limit their scope.
Use std::cerr to output errors.
_The stream is different to std::cout, so I could redirect the output and keep the errors
_Also cerr always flush, so if you print it, you will see it. (it took me a while to notice that the name of the file was "Map.img.xml" but you were trying to open "map.img.xml")
Last edited on
Ok, thanks guys I'll test it out. And yeah that "Map.img.xml" was just a forum typo...
And umm... yeah, I just tried ne555's method and I avoided system but it still freezes up...
Last edited on
Sorry, I cannot reproduce the problem. I've got segmentation fault, but setting a=b=0; there is some output (though I don't know if it is correct)
example output
67700000    Valefor Strolling Pat
67700000    Valefor Hiding Plac
67700000    Fog Fores
67700000    Astaroth Strolling Plac
67700000    Astaroth Hiding Plac


Could you update the code?
Topic archived. No new replies allowed.