File I/O Help

Hi there,

I have been trying to load certain file formats but have not had luck due to my lack of knowledge in file i/o. The 2 file formats are .obj and .ply.

I will show the file types and then explain how I'm going to load it and I hope you guys can point me in the right direction.


.ply:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ply
format ascii 1.0
comment Created by Blender3D 249 - www.blender.org, source file: 
element vertex 24
property float x
property float y
property float z
element face 6
property list uchar uint vertex_indices
end_header
1.000000 1.000000 -1.000000 
1.000000 -1.000000 -1.000000 
-1.000000 -1.000000 -1.000000 
-1.000000 1.000000 -1.000000 
1.000000 0.999999 1.000000 
-1.000000 1.000000 1.000000 
-1.000000 -1.000000 1.000000 
0.999999 -1.000001 1.000000 
1.000000 1.000000 -1.000000 
1.000000 0.999999 1.000000 
0.999999 -1.000001 1.000000 
1.000000 -1.000000 -1.000000 
1.000000 -1.000000 -1.000000 
0.999999 -1.000001 1.000000 
-1.000000 -1.000000 1.000000 
-1.000000 -1.000000 -1.000000 
-1.000000 -1.000000 -1.000000 
-1.000000 -1.000000 1.000000 
-1.000000 1.000000 1.000000 
-1.000000 1.000000 -1.000000 
1.000000 0.999999 1.000000 
1.000000 1.000000 -1.000000 
-1.000000 1.000000 -1.000000 
-1.000000 1.000000 1.000000 
4 0 1 2 3 
4 4 5 6 7 
4 8 9 10 11 
4 12 13 14 15 
4 16 17 18 19 
4 20 21 22 23 

for the .ply file what I want to do is read the entire file, then use a function to search for 'element vertex' and then extract the number beside it(24 in this case). Then I want to do the same but for 'element face'. after this since I know there are 24 vertices I will find the 'end_header' and read 24 lines after it(those coordinates). And then I want to use a for loop and seperate each coordinate and use .push_back() after converting it to put it in my vector.

for example for line 11:
 
1.000000 1.000000 -1.000000 


i'll use a for loop to pushback 1.000000 first then the next one (1.00000) and the last one(1.00000) and it'll do that for all the lines till the counter hits 24 or the number that has the # of vertices stored in it. After I do that it'll read the next 6 lines of the variable set for the # faces. Here I want to omit the 4 at the start of each line and then use the same for loop i used earlier to .push_back each face in a vector (0) then (1) then (2) then (3) etc..

and that's all I need for this one so to summarize how do I find a certain string in a file and then read the characters beside it till we hit a space.
Then after find another string get the index # of that line and then we will use that number and read lines using index's (for example reach everything on line 14). Finally make a for loop which will split up each element in that string. I know how to do this sort of use a function to read each character till it hit's a space and repeat that twice for each line.

Anyways sorry for this bulky post I have been trying to do this but i can't figure it out in c++. I know how to do this in python but that won't help....

here's my code if you need it it's not much.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <string>
#include <glew.h>
#include <vector>



using namespace std;
using std::vector;

int main()
{
	string line;
	string dummy;
	vector<GLfloat>vertex;

	char *content = "";

	int line_num = 0;
	int vertices_num = 0;
	int faces_num = 0;

	ifstream obj;
	obj.open("cube.ply",ios::in || ios::binary);
	
	

	while(!obj.eof())
	{
		getline(obj,dummy);
		
		line_num = line_num +1;
	}
	obj.close();

	cout << dummy<<endl;
	//double temp = atof(num.c_str());
	system("pause");
	return 0;
}


Can you get Blender3D's source code? If yes, reuse, or at least learn from.
Hmm I'm not sure but blender was written in python so it wouldn't matter because I'm familiar with python i/o and not c++. I just need a function that I can input a string and it'll return the index or location of that string
How about http://people.cs.kuleuven.be/~ares.lagae/libply/
(I just do websearches blindly.)

Note on your code: line 25 opens file as binary, but file looks like ascii.
Hmm I appreciate the help but I'm trying to do this one myself 6rom my own code, and thank you for pointing that out man!
Processing input is tedious. If someone has already written a library that does all the error checking, then it is better to use it, or at least look at how they do it. Some wheels are simply not worth reinventing, and you at least do learn how to link in third-party code (although it is very similar to using the standard library).

On the other hand, if the main purpose is just to learn programming, then a parser is a decent problem to work with.


One option is to use a state machine. When reading a new line the machine is in some state that determines how this line is used (which can change the state). For example, you are still in header mode when you get the "end_header". After that line you would be in "data" mode, but what you did find during header lines determines what to do with the data lines.
Yes, it is too learn and I finally have found the functions used I just used scanf() and getline it was easy after that,

Thanks anyways!
Topic archived. No new replies allowed.