you mean here that i must read string and i pars it into float insread to read directely the number as float ?
I'm not saying you "must" do anything. When you originally posted the thread your question was centered around istream::getline This is why I'm getting confused. Consider the code that started the thread. I will post corrected code below. It's a little program that simply reads lines in the file and spits them back out.
Code:
#include <iostream>
#include <fstream>
using namespace std;
void simpleRead();
int main() {
simpleRead();
return 0;
}
void simpleRead() {
std::ifstream file;
file.open("file.txt"); // This object assumes you want to read the file
if (file.is_open()) {
char lineContent[1000];
while (file.getline(lineContent, 1000)) {
cout << lineContent << endl;
}
file.close();
}
}
That's about as simple as I can make it without changing too much. std::string would be better to use here, though. Anyway ll of the string parsing you will have to do should take place in between lines 24 and 28.
You can in fact feed lineContent into a std::stringstream and treat that exactly like your file content. This is string parsing. It would make error handling easier for you. If the user makes a mistake in the file, on a particular line, you can write an error notice to cout and continue reading the file, for example. Either way, you need to decide if you want to read directly from the file or not. The two ways are mutually exclusive and you will end up with very different code either way.
The part that made me the most confused was this code.
Code:
std::istream & operator>> (std::istream & stream, pose & p)
{
// stream >>p.time;
printf ("p.timedans stre===>%f",p.time);
//p.time = 0;
std::copy(std::istream_iterator<joint_angle>(stream), std::istream_iterator<joint_angle>(), std::back_inserter(p.angle));
return stream;
}
I don't think you're doing this correctly. Did you test this? Soldiering on, I offered you an alternative method of parsing your file, which I felt was simpler and I will now explain.
Code:
// 1. read time information
// 2. read pose information
joint_angle ja;
vector<joint_angle> angles;
char c;
while ( stream >> ja >> noskipws >> c ) {
angles.push_back(ja);
if (c == '\n' ) {
stream >> skipws; // Reset - because of the reading we have to repeat (see 1)
// new pose incoming
// possibly break; here
}
}
It's a bit rough around the edges
I left out part one under the assumption that you knew what that step entailed.
Part two entailed getting all the angles in a particular pose. The noskipws modifier simply makes sure that the character c is given the value '\n' (which is a newline char) eventually. Please read the information on this page. If we didn't use it, we would end up in trouble, because white space is normally skipped and we depend on the information from newline to move to the next pose. This is in line with your thinking here:
-0.01 is pose[0].time , 0.03 0.04 -0.5 is pose[0].joint_angle[0]........
-0.03 is pose[1].time ,0.05 0.06 -0.005 is pose[1].joint_angle[0]...
After you do all the reading for one pose, you can add everything (the time information and the vector of joint_angle, wholesale) into a larger data structure. The good news is that you can use my way of reading joint angles whether you choose to do string parsing or not. The only things that would change is the type of stream, and the fact that you won't need to mess with the white space anymore.
The only way to make it any simpler would be to know the number of poses and joint angles possible, in advance, I think. Good luck.