Solved

Parsing out text files....

Posted on 2003-11-14
8
228 Views
Last Modified: 2010-04-01
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
Comment
Question by:Tiuq
8 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 25 total points
Comment Utility
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
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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
 
LVL 5

Assisted Solution

by:g0rath
g0rath earned 25 total points
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 13

Expert Comment

by:SteH
Comment Utility
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
 
LVL 5

Expert Comment

by:g0rath
Comment Utility
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
 

Expert Comment

by:Eelis
Comment Utility
// 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
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now