Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

Parsing out text files....

Currently i am trying to load in data that looks like this....

Code:
      points {
        (-12.201 -15.781 49.622) (-12.201 -15.781 76.876) (-12.201 12.3 49.622)
        (-12.201 12.3 76.876) (15.054 -15.781 49.622) (15.054 -15.781 76.876)
        (15.054 12.3 49.622) (15.054 12.3 76.876) (-8.3853 -13.774 87.493)
        (-8.3853 4.0987 91.257) (11.239 4.0987 91.257) (11.239 -13.774 87.493)
        (-2.2526 -10.082 96.412) (-2.2526 -3.3801 97.822)
        (5.1064 -3.3801 97.822) (5.1064 -10.082 96.412) (-9.6457 12.3 52.176)
        (12.499 12.3 52.176) (12.499 12.3 74.324) (-9.6457 12.3 74.324)
        (-9.6457 11.531 78.227) (12.499 11.531 78.227) (9.3989 4.8674 89.912)
        (-6.5455 4.8674 89.912) (-6.5455 3.3975 91.873) (9.3989 3.3975 91.873)
        (4.4165 -2.6789 97.207) (-1.5627 -2.6789 97.207) (-9.6457 9.6626 52.176)
      }

what i am using to load it looks like this.....

Code:
      for(int i = 0; i < 100; i++)
      {
            fgets(oneline,255,filein);
            
            if (!(strstr(oneline,"points {") == NULL))
                  break;      
      }

      int current = 0;
      for(i=0;i<5000;i++)
      {
            fgets(oneline,255,filein);      //readstr(filein,oneline);
            if (!(strstr(oneline,"      }") == NULL))
                  break;
            sscanf(oneline, "        (%f %f %f) (%f %f %f) (%f %f %f) \n", &vtemp[i].x, &vtemp[i].y, &vtemp[i].z, &vtemp[i+1].x, &vtemp[i+1].y, &vtemp[i+1].z, &vtemp[i+1].x, &vtemp[i+1].y, &vtemp[i+1].z);
            current += 3;

      }

There are 2 problems with this, first it doesnt work rigth at all, and second every once in awhile in the input file there are only 2 sets of points instead of 3. any help would be apreciated.
_
0
Tiuq
Asked:
Tiuq
2 Solutions
 
SteHCommented:
Replace the second for loop with a while loop and split each line inside the second loop to look for the closing brace ')'.

   int i =0;
  while (true) {
      fgets (oneline, 255, filein);
      if (!(strchr(oneline, '}')  != NULL)
            break;
       char* p;
       while (1) {
             p = strchr (oneline, ')');
             if (p == NULL)
                    break;
             sscanf (oneline, "%f%f%f", &vtemp[i].x, &vtemp[i].y, &vtemp[i].z)
             ++i;
       }
 }
0
 
SteHCommented:
Sorry not yet complete

       while (1) {
             p = strchr (oneline, ')');
             if (p == NULL)
                    break;
             sscanf (oneline, "(%f%f%f", &vtemp[i].x, &vtemp[i].y, &vtemp[i].z)
             ++i;
             oneline = p+1; // just increment oneline to point after ).
       }
0
 
g0rathCommented:
the one issue is your doing line by line, and this will cause an error

on one line you have 4 elements, and on the other you only have 2
 
so on the first you'll list the 4th element, and the next one you'll only have 2 points.

another method would be read the entire file into memory and then parse it in memory, the logic would be different but it may be easier to apply to a Class that you create.

int Points::LoadFromFile( fName )
void Points::OutputToScreen()

etc... just an idea
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SteHCommented:
I don't see the improvement if you change the location of the data from hard disk to memory. Unless you don't remove line breaks streaming doesn't change. But from the question I assumed that the number of points on a line can be 2 or 3 but all points have all 3 coordinates in one line. Otherwise one needs to store the remainder into a separate string and append the next line.
In that case before scanning one has to check for the next ')'. Reading from serial port/network etc requires this approach since it could always be that a single read doesn't get the terminating char which only follows with the next read or even later.
0
 
g0rathCommented:
Well I was thinking of things like

remove newlines in area, created char ** to each point, divide by three to make sure we have semi-accurate data...

Then you have something like

char *
     line*
     line*

using atoi or strtol...but I have a vector class that I wrote to do this kind of stuff...I usually read entire file into memory and perform all my operations in memory, but I'm also used to dealing with larger sets where you would see some performance increase.....but at this size it would be overkill...

Just offereing another point of view.
0
 
EelisCommented:
// This will do the trick:

#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

struct Point { float a, b, c; };

template <typename IS>
IS & operator>> (IS & is, Point & p)
{
  char c; // opening and closing brackets
  is >> c >> p.a >> p.b >> p.c >> c;
  return is;
}

int main ()
{
  vector<Point> points;

  ifstream f ("data.txt");

  string s;
  while (getline(f, s))
    if (s == "points {")
    {
      cout << "a\n";
      while (getline(f, s))
      {
        if (s == "}") break;
        istringstream iss (s);
        Point p;
        while (iss >> p) points.push_back(p);
      }
    }

  return 0;
}
0
 
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: SteH {http:#9746083} & g0rath {http:#9747792}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now