Text file parsing

Started by
14 comments, last by tmoneyksu 20 years, 10 months ago
Cool! I'll try that.

[edited by - tmoneyksu on June 5, 2003 12:23:45 PM]
Super-newb extraordinaire.
Advertisement
The proper C way of doing that is using fscanf to convert the characters as they're read, not afterwards with atoi and atof.

I strongly suggest you use C++ style I/O instead of C style, since C style comes with a lot of headaches. For example with C style every time you read a value from a file or from the keyboard you have to specify its type, like this:

// read an integer (%i) from fp into myVariable
fscanf(fp, "%i", &myVariable);

Now what if you change the type of myVariable to a float? You have to change "%i" all over the place or you're likely to get a nasty crash and/or incorrect values.

In C++ the syntax is just:

someFile >> myVariable;

The syntax is the same for any basic type like bool, int, float, double, etc. so you can change a variable's type transparently in your code.

The other big advantage is from reading data into structs/classes. You can override the >> operator for C++ style I/O and fill in a whole struct from a file in one swoop.

struct S{  int var1;  float var2;  bool var3;  // I forget the exact function signature but it's something like this  istream & operator >> (istream & input)  {    return input >> var1 >> var2 >> var3;  }};S s;someStream >> s;


Doing something like this with C style I/O is a complete pain, you have to read in each element individually. Even if you make a wrapper function that does it for you it will have a different syntax from the normal fscanf and it will suffer from the type problems I mentioned above.

I forgot to mention the C++ code I've been showing is part of what's typically called the C++ streams library. It's in standard headers like fstream, iostream. You should be able to find references all over the web if you want to learn.

[edited by - Dobbs on June 5, 2003 12:27:09 PM]
Okay, now I''m totally lost again. Let me supply some data to let you all know what I''m trying to do.

Named object: "Box01"Tri-mesh, Vertices: 8 Faces: 12Vertex List:Vertex 0:  X:0.000000    Y:20.250000    Z:10.125000Vertex 1:  X:0.000000    Y:0.250000    Z:10.125000Vertex 2:  X:20.250000    Y:20.250000    Z:10.125000Vertex 3:  X:20.250000    Y:0.250000    Z:10.125000Vertex 4:  X:20.250000    Y:20.250000    Z:-10.125000Vertex 5:  X:20.250000    Y:0.250000    Z:-10.125000Vertex 6:  X:0.000000    Y:20.250000    Z:-10.125000Vertex 7:  X:0.000000    Y:0.250000    Z:-10.125000Face list:Face 0: A:0 B:1 C:2 AB:1 BC:1 CA:1Smoothing: 1Face 1: A:1 B:3 C:2 AB:1 BC:1 CA:1Smoothing: 1Face 2: A:2 B:3 C:4 AB:1 BC:1 CA:1Smoothing: 2Face 3: A:3 B:5 C:4 AB:1 BC:1 CA:1Smoothing: 2Face 4: A:4 B:5 C:6 AB:1 BC:1 CA:1Smoothing: 1Face 5: A:5 B:7 C:6 AB:1 BC:1 CA:1Smoothing: 1Face 6: A:6 B:7 C:0 AB:1 BC:1 CA:1Smoothing: 2Face 7: A:7 B:1 C:0 AB:1 BC:1 CA:1Smoothing: 2Face 8: A:6 B:0 C:4 AB:1 BC:1 CA:1Smoothing: 4Face 9: A:0 B:2 C:4 AB:1 BC:1 CA:1Smoothing: 4Face 10: A:1 B:7 C:3 AB:1 BC:1 CA:1Smoothing: 4Face 11: A:7 B:5 C:3 AB:1 BC:1 CA:1Smoothing: 4


This is, of course, and .asc file. What I need to do is get the number of vertices, the vertex indices, the vertex coordinates, and the face data out of the file, and into structures in my program. What is the best way to go about doing this?
Super-newb extraordinaire.
Anyone?
Super-newb extraordinaire.
To find the number of vertices:

//buffer must be null terminated or strstr won't work properlychar* ptr = buffer;char temp_num[10];int num_vert, i;ptr = strstr(ptr, "Vertices:"); //not positive strstr will work if you assign the return value to the same pointer you pass in.ptr += 10; // ptr now points to the first digit in the number of vertices.i = 0;while(ptr[i] != ' '){ // search for the space after the value  temp_num[i] = ptr[i];  i++;}num_vert = atoi(temp_num, 10);


The same technique can be done for every variable in your file. Again, this isn't the ideal way to solve the problem, but it will work.

Jedyte: The original poster was the one who first posted C code so i'm just going with it.

As mentioned, the C style input has some drawbacks. It does, however, tend to be a little quicker than the C++ i/o. Either way, it's almost always easier to parse the file as you read it from the disk. For example, as reading in the vertex data from the disk, you can use a command like

fscanf(pASC, "X:%f\tY:%f\t%f\n", &x, &y, &z); //or the equivalent C++ type code

to read in an entire line of variables and convert them to floating point numbers instead of the cumbersome way of extracting them from a giant buffer.

[edited by - kdogg on June 6, 2003 1:10:47 AM]
This will be of no use to you at all probably, but does anyone know the Quake 3 Shader files? I wrote a few classes to parse something similar to that for my own projects and were an absolute god-send I tell you. The parsing routines aren''t that well written, but they work to great effect. Maybe it might be helpful to look at them?

The URL is http://www.gibbersoft.tk/ and it''s on the downloads page, titled Roller.
__________________________________Peter Lewis ([email=me_AT_pjblewis.com]E-mail[/email] | Portfolio)

This topic is closed to new replies.

Advertisement