problem after problem

so ive been messing round with c++ for about a year and i know ive got tons to learn and im planning on going to college for software development but as for now im a self taught programmer. With that said, please be patient with me.. im trying to write an OBJ loader for d3dx10. the way the file was written intimidated me a little so i wrote a function to organize it and make it easier to read through line by line. now im writing the function to read through the file, loading each line(x, y, z) into a d3dxvector3 vector and eventually load into a mesh but it says i dont have a null terminated string.. now i know that a string ends with a null so the pc knows thats the end of the string but when i stored it into a char * to stream through the string and break it down into x, y, and z i cant figure out how to add that null character.. i believe it would change my code drastically and i have no idea how else to do it... so thanks for the help in advance... also i have a second problem.. when i try to use the function it always throws an error to the file parameter and i dont understand the error so idk whats wrong:

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
42
43
44
45
46
47
48
49
50
51
52
53
void PrepareOBJ(ifstream inputFile, ofstream outputFile)
{
	string currentLine;
	if (inputFile.is_open())
	{
		while(!(inputFile.eof()))
		{
			getline(inputFile, line);
			
			for (int i = 0; i < currentLine.size(); i++)
			{
				if (currentLine.at(i) == 'v')
				{
					currentLine.at(i) = '\n';
				}
			}

			outputFile << currentLine;
		}
	}

	inputFile.close();
}

void LoadOBJ(vector <D3DXVECTOR3> Positions, ifstream File)
{
	float x = 0, y = 0, z = 0;
	char* slot1[10];
	char* slot2[10];
	char* slot3[10];
	stringstream ss(line);

	if (File.is_open())
	{
		while(!(File.eof()))
		{
			getline(File, line);
		
			ss.getline(slot1[10], 15, ' ');
			ss.getline(slot2[10], 15, ' ');
			ss.getline(slot3[10], 15, ' ');

			x = atof(slot1[10]);
			y = atof(slot2[10]);
			z = atof(slot3[10]);

			D3DXVECTOR3 vector(x, y, z);
			Positions.push_back (vector);
			cout << Positions[Pos] << "\n";
			Pos++;
		}
	}
}


i wanted this to be a header file i could use in my games so thats the reason for the functions
I've no clue about what D3DXVECTOR3 is; assuming that it is some type can be constructed from three float values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// void PrepareOBJ(ifstream inputFile, ofstream outputFile)
// streams are not copyable, pass by reference
void PrepareOBJ( ifstream& inputFile, ofstream& outputFile )
{
    // copy contents of input file to output file, replacing 'v' with '\n'
    char c ;
    while( inputFile.get(c) ) outputFile << ( c == 'v' ? '\n' : c ) ;
}

struct D3DXVECTOR3 { D3DXVECTOR3(float,float,float) ; /* ... */ };

// it doesn't make sense to pass the vector by value
void LoadOBJ( vector<D3DXVECTOR3>& Positions, ifstream& File )
{
    float x = 0, y = 0, z = 0;
    while( File >> x >> y >> z ) Positions.push_back( D3DXVECTOR3(x,y,z) ) ;
}
Last edited on
thank you for your help, but just for clarification, when i tried your prepare obj function it did nothing. mine was already working, but i dont understand how your function progressively makes its way through the file.. id like to understand your method just for learning purposes and yours is shorter. will get() continuously move to the next char in the file? i thought it would just get the first char.. unless maybe i gave it a position like using .at or something like it? (doesnt sound like a ? but it is lol) and secondly can you explain how:

while( File >> x >> y >> z ) Positions.push_back( D3DXVECTOR3(x,y,z) ) ;

will break this line into three variables??? " 2.675214 -2.638026 -1.912134"
i want it to read through each line.. load into a string, break into three variables(x, y, z) and store in a d3dxvector3.. btw you cant redefine the d3dxvector3 struct.. so instead of:

struct D3DXVECTOR3 { D3DXVECTOR3(float,float,float) ; /* ... */ };

it would be more like:

struct Vertices { <vector>D3DXVECTOR3 Positions(float, float, float); };

I'm not saying that's right, but a D3DXVECTOR3 is a type to store a vertex coordinate (float x, float y, float z). its part of Direct X.

yet again your help and time is appreciated
> prepare obj function it did nothing.

Well, I checked it now, and it does what it is expected to do.
http://coliru.stacked-crooked.com/a/50c92211e65c21c9


> will get() continuously move to the next char in the file?

Yes. Repeatedly calling get() will retrieve characters one by one till end of file.


> while( File >> x >> y >> z ) Positions.push_back( D3DXVECTOR3(x,y,z) ) ;
> will break this line into three variables??? " 2.675214 -2.638026 -1.912134"

Yes. http://coliru.stacked-crooked.com/a/c6459abd6e2fbc4f
ok THANK YOU. i got everything working. my only problem now is that i cant store the values in the vector. for instance it wont allow me to do this:

Positions.push_back(D3DXVECTOR3 Vertex(x, y, z);

it says type name not allowed.. so i tried:

1
2
D3DXVECTOR3 Vertex(x, y, z); 
Positions.push_back(Vertex);


and the errors went away but instead of Vertex = "2.675214 -2.638026 -1.912134"; it came back as "001EF850".... ???????
Topic archived. No new replies allowed.