Solved

Parsing out text files....

Posted on 2003-11-14
8
266 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
ID: 9746083
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
ID: 9746087
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
ID: 9747792
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 13

Expert Comment

by:SteH
ID: 9747895
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
ID: 9747964
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
ID: 9753664
// 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
ID: 10249182
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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