I have a code snippet i've been working on but not getting very far.

What I have is a text file which looks like an XML but i want to get the data between the tags and ignore the tags themselves. I want to count the number of data items between the tags and create a 2D array so if for example I have this:

<tag1>
Data 1
Data 2
Data 3
Data 4
</tag1>
<tag2>
test1
test2
test3
</tag2>

I would like to create an array 4 by 3.

I think i can get the first array right but when i try and get the other bit it gives the wrong answer

int d1=0, d2=1;
	
		while (! filename.eof()) //Loop through lines
		{
			getline(filename, line);
		
			string test = "<";
			if(line.find("<",0) == string::npos)
					cout << line << endl;
			
			unsigned int pos1 = line.find(sub1, 0);
			unsigned int pos2 = line.find(sub2, 0);
	
			if( pos1 = string::npos)
				if(pos2 != string::npos)
					d1++;
	
			if( pos1 = string::npos)
				if(pos2 = string::npos)
					d2++;

	
		}
	
		IntArrayPtr *m = new IntArrayPtr[d1];
	
		int i;
	
		for(i=0; i < d1; i++)
			m[i] = new int[d2];
	
		cout << d1 << endl;
		cout << d2 << endl;
		
		filename.close();

You should consider using a vector, or a list instead of bothering with an array. It would be much easier. Read the whole file line by line in a temporary string then push it back into the vector. Iterate through the vector and you will get all of the lines one by one. Then write a function, which can determinate if it is a tag(opening or closing). If it is just throw it away, or mark it with something. When you are done, all you have to do is read out, the unmarked strings. Oops I forgot, it would only work if the tags are separated with new line, otherwise you would have to make some substraction from the strings with tag.

You should split up all the tasks into separate functions. All of it should be part of class.

The file handling would probably look like this. Unless you're concerned about massive files..

if(file.is_open)
    {
         while(getline(file, str))
         {
              lines.push_back(str);
         }

         file.close();
    }

You now have the contents.
Time to just get where a tag ends and begins, continue to alter the attributes of the inner-most data based on these tags.

I did think of using seperate functions to break down each bit so i'll look into that.

Also with vectors can they be 2D?

The reason i thought an array would be better becuase i can count the number of tags so i can set the array size but i need to get wheat is in between.

Also i need to be able to reference a location of a cell and disp[lay what is in there.

I have only just started using C++ so i have no clue but i have used Java before a little

2D enough for you?

vector< vector<int> > v;

    for(int i = 0; i < 10; i++)
        v.push_back( vector<int>() );

    for(int i = 0; i < v.size(); i++){
        for(int x = 0; x < 10; x++){
            v[i].push_back(x*i);
        }
    }

    for(int i = 0; i < v.size(); i++){
        for(int x = 0; x < v[i].size(); x++){
            cout << v[i][x] << '\t';
        }
        cout << endl;
    }

Chris

That's just wrong information! A vector can hold anything - its a freakin template class!

Yeah now I know. I am sorry I didn't want to mislead anyone.

This article has been dead for over six months. Start a new discussion instead.