0

I have a txt file with the following data:

Point2D, [3, 2]
Line3D, [7, 12, 3], [-9, 13, 68]
Point3D, [1, 3, 8]
Line2D, [5, 7], [3, 8]

How do i actually go about storing them by removing multiple delimiters so i could extract the data out?

What i want is to read into the first line and ignore "," "[" and "]" so i could store Point2D, 3 and 2 individually. Then i go on and proceed for the second line and so on.

Also, is it possible to do this, let say for example:

The first line "Point2D, [3, 2]", when Point2D is being detected, it will store 3 and 2 into point2d.x and point2d.y.

For the second line "Line3D, [7, 12, 3], [-9, 13, 68]", it will store into the values into line3d.x,line3d.y,line3d.z and so on accordingly.

Right now i could only get it to ignore ','. This is what i've done so far:

void readData()
{
    string fileName;
    int i=0;
    cout << "Enter file name: ";
    cin >> fileName;
    fstream infile;

    infile.open(fileName.data());
    // This will count the number of lines in the textfile.
    if (! infile.is_open())
    {
        cerr<<"Error : " << fileName.data() <<" is not found"<<endl;
    }

    string line;    
    stringstream field;
    while (getline(infile,line))
    { 
        string f;
        field<<line;
        while (getline(field,f,','))
        {
            recordA.push_back(f);              
        }
        field.clear();
    }
    cout << recordA.size() << " records read in successfully!";
    infile.close();

}

  • I posted an [answer](http://stackoverflow.com/a/2909187/179910) to a somewhat similar question some time ago. It looks like the same basic idea would work here. If you really don't want to explicitly read the commas and brackets, you could create a locale that treats them as whitespace. [An example](http://stackoverflow.com/a/10060244/179910) that does that with slashes and dashes. – Jerry Coffin Nov 16 '12 at 03:48
  • Were you here before and just made a new account? This question looks *very* similar to a [string of questions](http://stackoverflow.com/users/1578897/user1578897?tab=questions) that was asked not long ago... – Kerrek SB Nov 16 '12 at 03:49
  • i took a look at that user, he seem to be doing the same thing as me(might be my classmate), but i can assure u we are two different person – user1793001 Nov 16 '12 at 03:55

1 Answers1

0

To complicate your life, I suggest the following:

  1. Make a factory object that reads in the text and creates an object based on the text. For example, when "Point2D" is read it creates a Point2D instance.
  2. Create methods in each class to read their own data. For example, Point2D would have a method to parse "[3,2]" and assign 3 to the first ordinate and 2 to the second ordinate.
  3. In the factory, have the object read the rest of the line and assign its members from the text line.
  4. If you make all the objects from a common parent object with a "load from file" method, you could have the factory invoke the method using a "generic" pointer to the parent.

Simplicity. Let the objects read in their own data.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154