Solved

simple parsing question

Posted on 2001-07-30
7
220 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 50 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

688 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