Solved

Parsing out text files....

Posted on 2003-11-14
8
256 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

777 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