Solved

simple parsing question

Posted on 2001-07-30
7
182 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now