point next object in a file

Dec 1, 2008 at 8:38pm
Hello everybody!
I am here to ask you some help one more time.I have a file woch contains string.This string is "separated" in substrings which represent an object(each substring).I have a method to read those objects and another that does some operations on them.All I want is to find a way to show which object is going to be read next.
Ask anything you cant understand.
Thanks in advance.
Dec 1, 2008 at 9:05pm
I would put all the substrings into a vector or other std:: container, then just use an iterator to get the next one. (Just be careful of modifying the container, it will invalidate your iterators)
Dec 1, 2008 at 9:14pm
Yes,I have thought this kind of solution but I cant use vector,only array.If I can find a way to find its size it will be great.But substrings dont have a constant number of chars:s
Do you have some other ideas?
Dec 1, 2008 at 9:18pm
Use a dinamic array of strings
Dec 2, 2008 at 6:33am
Hmm,I am not sure what do you mean.Can you give me a small example,please?
Dec 2, 2008 at 1:07pm
Of course a standard container is better than this, but without STL I would do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <fstream>
#include <string>
unsigned int s = 1; // size of your array
string *array = new string[s];
void IncreaseArray( unsigned int amount = 1 )
{
     string *copy = new string[s];
     int n;
     for (n=0; n<s; n++)
          copy[n]=array[n];
     delete[] array;
     s += amount;
     array = new string[s];
     for ( n-- ; n>=0; n--)
          array[n]=copy[n];
     delete[] copy;
}

then call IncreaseArray each time you want to add elements to array
Last edited on Dec 2, 2008 at 1:07pm
Dec 2, 2008 at 3:03pm
Ok,I ve got it.Thanks a lot bazzy
Topic archived. No new replies allowed.