Selectively read text file using keywords

I am attempting to make a program that reads an STL (stereolithography) file for its facet coordinates.
A typical block of an STL file looks like this:

facet normal 2.64 5.54 3.55
outer loop
vertex 2.4 6.8 4.3
vertex 2.5 6.9 4.4
vertex 2.6 7.0 4.5
endloop
endfacet

I would like to extract and print the points given in the "facet normal" line, while ignoring everything else.
Is there a way that I can extract the content between "normal" and "outer" for each one of these blocks, associate each with separate x, y, and z float variables, and print them in an orderly manner?
Last edited on
What do you mean?
Where is your code?
I thought I had provided enough information, but my apologies.
Can you be more specific on what you're not understanding?
I didn't include the code because I'm at a complete loss as to how I would accomplish this, so it does little more than open the file and print its contents to the command line.
I haven't reached the point where I'd be getting specific errors, either.
Let me know if you'd like me to include my code anyway and I'll gladly add it.
Let me know if you'd like me to include my code anyway and I'll gladly add it.

Show us the efforts you have put into the assignment.
Last edited on
If Sakurawhatever cannot understand a simple question, his majesty should not be sent a private message.

EDIT:
Show us the efforts you have put into the assignment.

Maybe he should send you a private message? Why did you edit your post?

----------------------------------------
@OP: If all blocks look exactly like this (i.e. exactly 7 lines), below would be a good solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream file("file.txt");
    double x, y, z;

    file.ignore(256, ' '); // ignore until space
    file.ignore(256, ' '); // ignore until next space;
    file >> x >> y >> z;
    while(file)
    {
        std::cout << "Numbers are: " << x << " " << y << " " << z << "\n";
        // skip next 6 lines
        for(int i = 0; i < 6; i++) file.ignore(1000, '\n');

        file.ignore(256, ' '); // ignore until space
        file.ignore(256, ' '); // ignore until next space;
        file >> x >> y >> z;
    }
}
Last edited on
Arslanwhatever : Pardon me, I am actually not very good when it comes to Mathematics stuff. Some words are completely new to me.
Thank you very much Arslan, I'll be trying it in just a second.
Sakurasou, no sweat if the mathematics is going over your head. I should have clarified the technical language.

Edit:
Arslan, I think I need to explain how the file is structured a bit more clearly, since the solution you gave returns no output when I test it. Here is a better explanation.
Every STL file begins with:
solid name

This block repeats thousands of times within the average file:
facet normal x y z
outer loop
vertex v1x v1y v1z
vertex v2x v2y v2z
vertex v3x v3y v3z
endloop
endfacet

Every STL file ends with:
endsolid name

As I feared, I think this is going to be more complex than looking for spaces, and might require a ton of nested if statements to sort these blocks into the output needed.
Of course this is all speculation - I don't have the experience needed to put my thoughts into code quite yet.
Thanks for the quick replies from everyone, hopefully this isn't too much of an obstacle.
Last edited on
Every STL file begins with:
solid name

So this is new information. From what I understand, the file simply begins with two words "solid name" before the blocks of 7 lines start. If thats the case, only a simple modification to the code needs to be made.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream file("file.txt");
    double x, y, z;

    // ignore first twp words in the file
    file.ignore(256, ' '); // ignore until space
    file.ignore(256, '\n'); // ignore until newline

    file.ignore(256, ' '); // ignore until space
    file.ignore(256, ' '); // ignore until next space;
    file >> x >> y >> z;
    while(file)
    {
        std::cout << "Numbers are: " << x << " " << y << " " << z << "\n";
        // skip next 6 lines
        for(int i = 0; i < 6; i++) file.ignore(1000, '\n');

        file.ignore(256, ' '); // ignore until space
        file.ignore(256, ' '); // ignore until next space;
        file >> x >> y >> z;
    }
}


I made a sample file with 3 blocks and here was the outcome:

Numbers are: 2.64 5.54 3.55
Numbers are: 2.64 5.54 3.55
Numbers are: 2.64 5.54 3.55

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.


Note that the numbers are all the same because I simply made the same block 3 times over (copy-paste) for testing purposes.

The test file, btw, looks like this:

solid name

facet normal 2.64 5.54 3.55
outer loop
vertex 2.4 6.8 4.3
vertex 2.5 6.9 4.4
vertex 2.6 7.0 4.5
endloop
endfacet

facet normal 2.64 5.54 3.55
outer loop
vertex 2.4 6.8 4.3
vertex 2.5 6.9 4.4
vertex 2.6 7.0 4.5
endloop
endfacet

facet normal 2.64 5.54 3.55
outer loop
vertex 2.4 6.8 4.3
vertex 2.5 6.9 4.4
vertex 2.6 7.0 4.5
endloop
endfacet

ensolid name

Last edited on
It works!
Can't believe I got a problem solved in a single night, thanks so much.
Topic archived. No new replies allowed.