Solved

simple parsing question

Posted on 2001-07-30
7
209 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

831 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