?
Solved

simple parsing question

Posted on 2001-07-30
7
Medium Priority
?
226 Views
Last Modified: 2010-04-02
i'm reading data from a text file which basicaly contains floating points feagures in 2 columns.
however, sometimes in each row there are additional data which need to be read, for example:

double mass, score;

12.454    23446.67867
122.543   1116.80007
82.093    8818.20202   from spectrum Y-12.txt

     ifstream in(strFileName);
     in.exceptions(ios_base::failbit);

     double mass, score;
        vector<double> arrMass, arrScore;
//contains the double feagures

        vector<string> arrTextData;
//contain the additional text data

     char strLine[128];
     
     try
     {
          while(!in.eof())
          {
               in.getline(strLine, 128);
//How to read the data corrctly, there are only spaces between the data and the additional text data is not added in every row.
         
          }
     }
     catch (const ios_base::failure &f)
     {
          if(!in.eof())
          {
                        ErrorMsg(strFileName);
               return;
          }
     }


your help is appreciated....

0
Comment
Question by:sedgwick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 6334194
Really the best way to do this would be to look for the spaces in the file, and separate the tokens that way.  Here's an example that might work for you:
  ifstream input_file("Text1.txt");
  double mass, score;
  string line, last_part;

  while(getline(input_file, line))
  {
    int space = line.find(" ");
   
    mass = atof(line.substr(0, space).c_str());
   
    int second_space = line.find(" ", space + 1);
   
    if(second_space == string::npos)
      score = atof(line.substr(space, string::npos).c_str());
    else
      score = atof(line.substr(space, second_space - space).c_str());
 
    if(second_space != string::npos)
      last_part = line.substr(second_space + 1, string::npos);

    //Do your vector.push_back stuff here
  }
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 6334499
If the data always occurs at the end of the line then you can use >> to read the two floating point numbers.  Then you can use getline to read the rest of the line.  (This will be either just a \n character or some of that extra text and then a \n character).

For example

double d1,d2;
.
while(!in.eof())
{
   in >> d1 >> d2;
   in.getline(strLine, 128);
}


Note that for the best way to read a line you should use a string object and the global getline(), like

string RestOfLine;
getline(in.RestOfLine);

This will read a line of any length, wheras your approach is limited to 127 characters.
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6334518
it crashes dude, did u check on a real file,
like this one?

728.1627      6359.0000   XY-01
744.4961      6354.0000
766.4911      6997.0000
782.4509      6132.0000   Y-12
802.1027      6160.0000
816.4961      6004.0000
840.1649      6892.0000   X-77
842.6332      6391.0000   X-892
845.2565      7124.0000   Y-11


cheers

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 6334531
It crashes?   There is no way that the code I posted could crash.   Not for any faulty input.  At worst it will stop reading input, but it will not crash.

Post the exact code you used.
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6334601
thanks nietod
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6334609
thanks nietod
0
 
LVL 42

Author Comment

by:sedgwick
ID: 6334613
nietod: the comment wasn't for u, thanks again.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question