Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

simple parsing question

Posted on 2001-07-30
7
Medium Priority
?
234 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:Meir Rivkin
[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:Meir Rivkin
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
Technology Partners: 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:Meir Rivkin
ID: 6334601
thanks nietod
0
 
LVL 42

Author Comment

by:Meir Rivkin
ID: 6334609
thanks nietod
0
 
LVL 42

Author Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  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 …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

636 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