Help reading a file and picking out numerical values
Hello everyone,
So what i'm trying to do is read a file which contains both characters and numbers
I'm kinda new to c++ so bear with me a little bit :P
So i cast the lines into strings with getline and then try and convert that with sscanf . In order to do that though i must make a distinction between lines. lines starting with f are for inteer values whereas those starting with v are for floating. The code seems stuck once i try to run it somewhere and i can't figure out where. I'll include both a sample of the file and my code. any feedback is much appreciated!
Code:
===================================
#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++;
}
else 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;
}
=================================
File Sampple :
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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]