Error with sscanf help

Hi, I'm trying to read an obj file which contains a mixed amount of info of types text, float and integers in columns. I am using getline to import each line and then pick out the numerical values separately. But it seems to get stuck somewhere.

a Sample of what i'm trying to read is this ( actual file is 23k lines or so ) :
1
2
3
4
5
v 10.20 25.51 98.99
v 55.11 66.22 25.11
f 12 58 23
f 15 07 95
f 18 12 95 


And my code is this :
Ignore some counter values that i have placed as checkpoints now and then those are just for debugging. I'm new to c++ so it's some wrong definition on my part. All help would be appreciated!



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main ()
{	
	ifstream myobject("Model_1.txt"); 
	string line;										
	if(!myobject.good()) {
		cout << " Problem reading Object file! Aborting. ";
	}
	int number_of_lines = 0,counter,i;
   	while ( getline(myobject, line) ){
      	++number_of_lines;
   	}
	myobject.close();
   	myobject.open("Model_1.txt");
   	cout << 1 <<endl;
   	
   	int   Buffer_float_vector[3];
  	float Buffer_int_vector[3];
    float Coordinate_Matrix[number_of_lines][3];
    
    cout << 2 << endl;
    
	counter=0;
	while(!myobject.eof() ) 
	{
			getline(myobject, line);
			
			if (line.c_str()[0] == 'v'){
				line[0]=' '; // set pointer to 0 at the 1st character found
				sscanf(line.c_str(),"%f %f %f", 	&Buffer_float_vector[0],
													&Buffer_float_vector[1],	
										   			&Buffer_float_vector[2]);					   			
				cout << counter << endl;
				cout << Buffer_float_vector[0] <<" "<< Buffer_float_vector[1] <<" "<< Buffer_float_vector[2] << endl;  //
				for(i=0;i=2;i++){
					Coordinate_Matrix[counter][i]=Buffer_float_vector[i];
					Buffer_float_vector[i]= ' ';  // 
				}
				counter++;
			}		
			if (line.c_str()[0] == 'f'){
			line[0] = ' ';	 
			 sscanf(line.c_str(),"%i %i %i",								
					&Buffer_int_vector[0],										
					&Buffer_int_vector[1],									
					&Buffer_int_vector[2] );
					
			cout << counter << endl;	//	
			 for(int i=0;i=2;i++){
					Coordinate_Matrix[counter][i]=Buffer_float_vector[i];
					Buffer_int_vector[i]=' ';
				}
			counter++;							
			}
			else{
			counter = -1;
			}

	}	
	
	cout << " First Matrix Element : " << Coordinate_Matrix[1][0] << " || " << Coordinate_Matrix[1][1] 
									   << " || " << Coordinate_Matrix[0][2] << endl;
	cout << " Last Matrix Element : "  << Coordinate_Matrix[number_of_lines][0] << " || "
									   << Coordinate_Matrix[number_of_lines][1] << " || " 
									   << Coordinate_Matrix[number_of_lines][2] << " || " << endl;
	cout << " counter = " << counter;
	myobject.close();	
    return 0;
}


Thanks in advance!
Mario
It doesn't open the file the 2nd time and you have no check to see if it does or not. Try this and see if you can figure it out.



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main ()
{	
	ifstream myobject("Model_1.txt"); 
	string line;										
	if(!myobject.good()) 
    {
		cout << " Problem reading Object file! Aborting. ";
	}
	int number_of_lines = 0,counter=0,i=0;
   	while ( getline(myobject, line) )
    {
      	++number_of_lines;
    cout << number_of_lines << " " << line << endl; // Test
   	}
	myobject.close();

cout << "*************************************************************" << endl;
/*************************************************************/	
   	myobject.open("Model_1.txt");
//   	cout << 1 <<endl;
	if(!myobject.good()) 
    {
		cout << " Problem reading Object file! Aborting. " << endl;
		return 1;
	}   	

   	int   Buffer_float_vector[3];
  	float Buffer_int_vector[3];
    float Coordinate_Matrix[number_of_lines][3];
    
//    cout << 2 << endl;
    
	counter=0;
	while(!myobject.eof() ) 
	{
			getline(myobject, line);
    cout << number_of_lines << " " << line << endl; // Test			
			if (line.c_str()[0] == 'v'){
				line[0]=' '; // set pointer to 0 at the 1st character found
				sscanf(line.c_str(),"%f %f %f", 	&Buffer_float_vector[0],
													&Buffer_float_vector[1],	
										   			&Buffer_float_vector[2]);					   			
				cout << counter << endl;
				cout << Buffer_float_vector[0] <<" "<< Buffer_float_vector[1] <<" "<< Buffer_float_vector[2] << endl;  //
				for(i=0;i=2;i++){
					Coordinate_Matrix[counter][i]=Buffer_float_vector[i];
					Buffer_float_vector[i]= ' ';  // 
				}
				counter++;
			}		
			if (line.c_str()[0] == 'f'){
			line[0] = ' ';	 
			 sscanf(line.c_str(),"%i %i %i",								
					&Buffer_int_vector[0],										
					&Buffer_int_vector[1],									
					&Buffer_int_vector[2] );
					
			cout << counter << endl;	//	
			 for(int i=0;i=2;i++){
					Coordinate_Matrix[counter][i]=Buffer_float_vector[i];
					Buffer_int_vector[i]=' ';
				}
			counter++;							
			}
			else{
			counter = -1;
			}

	}	
	
	cout << " First Matrix Element : " << Coordinate_Matrix[1][0] << " || " << Coordinate_Matrix[1][1] 
									   << " || " << Coordinate_Matrix[0][2] << endl;
	cout << " Last Matrix Element : "  << Coordinate_Matrix[number_of_lines][0] << " || "
									   << Coordinate_Matrix[number_of_lines][1] << " || " 
									   << Coordinate_Matrix[number_of_lines][2] << " || " << endl;
	cout << " counter = " << counter;
	myobject.close();	
    return 0;
}


Changed your code around a little and it looks like it works but only for the first line. Seems like the easy fix is to count the lines as you process the data.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main ()
{	
	ifstream myobject("Model_1.txt"); 
	string line;										
	int number_of_lines = 0,counter=0,i=0;
/*	if(!myobject.good()) 
    {
		cout << " Problem reading Object file! Aborting. ";
	}
	int number_of_lines = 0,counter=0,i=0;
   	while ( getline(myobject, line) )
    {
      	++number_of_lines;
    cout << number_of_lines << " " << line << endl; // Test
   	}
	myobject.close();
*/
cout << "*************************************************************" << endl;
/*************************************************************/	
//   	myobject.open("Model_1.txt");
//   	cout << 1 <<endl;
	if(!myobject.good()) 
    {
		cout << " Problem reading Object file! Aborting. " << endl;
		return 1;
	}   	

   	int   Buffer_float_vector[3];
  	float Buffer_int_vector[3];
    float Coordinate_Matrix[number_of_lines][3];
    
//    cout << 2 << endl;
    
	counter=0;
	while(!myobject.eof() ) 
	{
			getline(myobject, line);
    cout << number_of_lines << " " << line << endl; // Test			
			if (line.c_str()[0] == 'v'){
				line[0]=' '; // set pointer to 0 at the 1st character found
				sscanf(line.c_str(),"%f %f %f", 	&Buffer_float_vector[0],
													&Buffer_float_vector[1],	
										   			&Buffer_float_vector[2]);					   			
				cout << counter << endl;
				cout << Buffer_float_vector[0] <<" "<< Buffer_float_vector[1] <<" "<< Buffer_float_vector[2] << endl;  //
				for(i=0;i=2;i++){
					Coordinate_Matrix[counter][i]=Buffer_float_vector[i];
					Buffer_float_vector[i]= ' ';  // 
				}
				counter++;
			}		
			if (line.c_str()[0] == 'f'){
			line[0] = ' ';	 
			 sscanf(line.c_str(),"%i %i %i",								
					&Buffer_int_vector[0],										
					&Buffer_int_vector[1],									
					&Buffer_int_vector[2] );
					
			cout << counter << endl;	//	
			 for(int i=0;i=2;i++){
					Coordinate_Matrix[counter][i]=Buffer_float_vector[i];
					Buffer_int_vector[i]=' ';
				}
			counter++;							
			}
			else{
			counter = -1;
			}

	}	
	
	cout << " First Matrix Element : " << Coordinate_Matrix[1][0] << " || " << Coordinate_Matrix[1][1] 
									   << " || " << Coordinate_Matrix[0][2] << endl;
	cout << " Last Matrix Element : "  << Coordinate_Matrix[number_of_lines][0] << " || "
									   << Coordinate_Matrix[number_of_lines][1] << " || " 
									   << Coordinate_Matrix[number_of_lines][2] << " || " << endl;
	cout << " counter = " << counter;
	myobject.close();	
    return 0;
}
Last edited on
Topic archived. No new replies allowed.