Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Parsing out text files....

Posted on 2003-11-14
8
Medium Priority
?
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 100 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 100 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
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.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.
Suggested Courses

730 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