Hello guys. I am having a problem reading in a file with both floats and characters or strings. I want to only extract the floating point values from the file. Here is how the file looks. It is a text file.
He went home 953.2654 696.54 963.42
685.262 5483.45 3653.5
5953.56 56.244 0.456
053.524 -43.65 486.5
The problem is reading that I am reading in floats and "He went home" are characters. I can I just read in the floating point values. Please keep programming responses to an introductory level. Thanks guys.
//in a loop.. (f is a float, file is the file)
if(file >> f){//if file does not contain a float there, this returns 0
//do whatever you want to do with the float
}else{
file.clear();//clear error flags
//now you have a choice:
string rubbish;//1. use >> to clear the stream. the disadvantages are that it uses
file >> rubbish;//unnecessary variable and it will only detect floats after a gap
//2. do it yourself:
for(;;){//advantage: you can decide how exactly it should act.
char ch = file.peek();//checks the next char
if(ch >= '0' && ch <= '9' || ch =='.' || ch == '+' || ch == '-') break;//if it could be the first char
//of a number, exit this loop
else file.get();//else, remove it from file.
}
}
This is what I am actually reading in. I want to only extract the floating point numbers from an stl file. Here is an example of how some of the data in a text file would look.
I only want to read in the floating point values, and ignore the other characters or strings
Here is my output when I just read in a file contain nothing but the floating put values. That comes out fine as long as I have floating point values. When I have strings in it the program doesn't even read in anything do to inFile>>*num which is reading in a float reads in a character.
I understand how to read in the file and everything, but I don't understand how to seperate reading in a string and floats together and seperating the floating point values. He is my code below
//This file has been used to read in both floating point values from STL and VTK files with only floating point values.
//It will read in the data then write it to a desired text file. Note that the first 2 lines of
//the Vtk file was deleted for its original text file, so that it can be read.
//Figure out why it doesn't read if first line is present.
//The formated out put was read from myfile.txt into BunnyData2.txt
ifstream inFile;
// ofstream my("C:\\Users\\Alanzo Granville\\Desktop\\BunnyData2.txt"); //the name of the object is my and the file that the data will go to is Myfile.txt
//*************IMPORTANT: BunnyData2 contains every formated vertex
//Now we have to tell the machine what to write to the file my, it is similar to doing cout<<"Output":, but we do my<<"Output";
//my<<"Testing for bunny stuff1"<<endl;
inFile.open("C:\\Users\\Alanzo Granville\\Desktop\\BunnyDataSTLRead.txt");
//I may have to put file address in here
if (!inFile.is_open()) {
cout << "Unable to open file"; //C:\Users\Alanzo Granville\Documents\Visual Studio 2008\Projects\Opening_A_File_Program\Opening_A_File_Program
exit(1); // terminate with error
}
float *num = NULL; //Initializing the pointer that holds the floats
int NumberOfFloats = 1; //used for later purposes
/*
//**********************************TEST Variables******************************************
//The objective this section is to create a basic character f to see if it uses up my memory
//***********This section counts my floats and triangles within the file********************
*/
int Tris = 1; //this variable will be used to control the for loop meaning number of triangles
/*
// **********************Formatting Section using Character and String Pointers**************
*/
int w = 0; //another counter in order to track
int i =1; //(Main function float counter)keeps track of number of times it goes through the loop
//***********
//Currently this while loop with dynamic memory allocation can read 833,412+ floats in no problem.
//It can read all of the myfile.txt file on desktop. Contains 277804 vertices
//***************
float x[25000][3]; //this array here should give at least one whole triangle
float y[25000][3]; //The second number indicates the number of triangles
float z[25000][3]; //The first number indicates the number of floating point values
for (Tris=1; Tris<=1;Tris++){ //this for loop controls w which should give Tris = 69451 works
for (i=1; i<13;i++)
//(Test case)for every 4 you get one triangle with a normal vertex and 3 vertices
{ //brackets go with the while loop originally
//***********************************Test section*****************************************
//if(inFile >> *num){//if file does not contain a float there, this returns 0
//do whatever you want to do with the float
//}
/*
//***************************Main Program Do not delete**************************************************
*/
//char ch = inFile.peek(); //.peek() lets me look into the file looking for char
//cout<<"ch is "<<ch<<endl; //This only seems to check the file at the beginning
//Test section above
int wCounter=3;
if(w%wCounter == 1 && w !=1)
{
w=0;
}
//This section alone is enough to read in all the floating point data for the vertices
num = new float; //Here I need to input characters format GLVertex3f(xf,yf,zf) or GLfloat vertices [35947][3] = 69451 works
inFile>>*num;// {{float numberf, float numberf, float numberf},..., {repeat...}}
//cout<<*num<<" "<<endl; // original code
switch(i%3)
{
case 0: //This case gives the 3rd component which is the z
z[i][w] = *num;
cout<<z[i][w]<<' ';
w++; //increments w after the 3 vertex is read
//i++;
cout<<endl; //this is temporary it can be deleted later
break;
case 1:
x[i][w] = *num;
cout<<x[i][w]<<' ';
//i++;
break;
case 2:
y[i][w] = *num;
cout<<y[i][w]<<' ';
//i++;
break;
} //end to switch statement
delete num;
//*****This section reads all float vertices in the bunny vtk file or stl
} //Ends inside for loop
cout<<endl;
//w++;//this section is for the outer for loop
} //ends other for loop
Thanks for the help man. I figured it out. I used inFile.peak() to check my characters then used conditional statements to read in my stuff. Did you know that you can use inFile.ignore("number of characters to ignore", "stopping criteria, i.e. '\n\"). Maybe I could have used that all along and just made the program ignore any characters other than the floating point values. Thanks for the help it was very useful.