sting member variable and sscanf function

I have created two classes:

1
2
3
4
5
6
7
8
9
10
11
class dm_Step
{
public:
	char m_plus;
	char m_particle;
	float m_energy_sec;
	float m_xpos_sec;
	float m_ypos_sec;
	float m_zpos_sec;
};



1
2
3
4
5
class dm_Track
{
public:
	vector<dm_Step> m_track;
}



I have a data base with hundreds of lines of code in the following format:

+ gamma 2.306013 -0.213225 0.928826 0.303014
+ electron 1.640281 0.197209 -0.866881 -0.457850
+ gamma 2.472920 -0.760897 0.647127 -0.047567
+ proton 18.087228 0.834184 -0.333168 0.439472
+ neutron 1.765848 0.872917 -0.381746 -0.303787
+ proton 38.739559 0.927977 0.165262 0.333986

I need to use my class to read in each line of the code using the sscanf function. Here is what I have so far:

1
2
3
4
5
6
7
8
9
10

dm_Track current_track;

while (myfile.good())
	{

         getline(myfile, line);
         sscanf(line.c_str(), "%c %c %f %f %f %f %f", &(current_step.m_plus), &(current_step.m_particle), &(current_step.m_energy_sec), &(current_step.m_xpos_sec), &(current_step.m_ypos_sec), &(current_step.m_zpos_sec));
          current_track.m_track.push_back(current_step);
        }


I need to do it this way so I can access various part of the data base. For example, if I wanted to know the type of particle on the second line of the data data base I could do:

 
cout << "particle_sec_line = " << current_track.m_track.at(1).m_particle << endl;


which would print 'electron'. When I try to this, it will only print the first character of the particle name. More significant however, when I try to access m_energy_sec it will print something like 8.0e9 which just seems like an uninitialized value. Is anybody familiar with the sscanf function and can tell me what I can do to fix this problem? I know there might other ways to do this but the way the rest of my code is setup, it would make it much simpler to do what I need to do using this exact method.



Last edited on
I think you should define member m_particle as a character array, for example, char m_particle[20]; and use format %s in sscanf for this member of the structure.
Why scanf and not >>?
Topic archived. No new replies allowed.