tags:

views:

118

answers:

5

Hi,

I am new to c++ programming. I am trying to read data in a file whose contents are as follows:

AS G02  2009 01 30 00 00  0.000000  2    1.593749310156e-04  4.717165038980e-11
AS G03  2009 01 30 00 00  0.000000  2    3.458468649886e-04  4.542246790350e-11
AS G04  2009 01 30 00 00  0.000000  2   -3.176765824224e-04  2.733827659950e-11
AS G05  2009 01 30 00 00  0.000000  2   -6.126657874204e-04  3.269050090460e-11

I would then write this data to an output file for processing later. The output should like this:

02  2009 01 30 00 00  0.000000  2    1.593749310156e-04  4.717165038980e-11
03  2009 01 30 00 00  0.000000  2    3.458468649886e-04  4.542246790350e-11
04  2009 01 30 00 00  0.000000  2   -3.176765824224e-04  2.733827659950e-11
05  2009 01 30 00 00  0.000000  2   -6.126657874204e-04  3.269050090460e-11

Can anyone help. Regards

A: 

google covers this ad nauseum. please use the phrases "file io" or "streams" in conjunction with "c++" or "cpp".

beyond that, if you have any more specific questions, you need to ask them.

San Jacinto
This is a comment, not an answer. Most things can be found on Google, that doesn't mean people should not ask those questions here. The whole point is to build a single repository for information. -1
Ed Swangren
well heck, let's just eliminate SO and just use Google then, eh? this HAS been covered there several times. there is no need to create a new question for this.
San Jacinto
+1  A: 

Read the file line by line and replace "AS G" with an empty string. Common, it'll be more fun if you try to do it yourself (not mentioning that you'll learn much more this way).

For example code and the basics you need for this, look at this discussion and the documentation of string replace.

schnaader
I don't think the OP knows how to do the basic input/output though.
Ed Swangren
+1  A: 

Do you NEED to use C++? If not, then Perl or any other similar tool/language would be a lot easier (and I'm a C++ developer)

Tim
Yep. Perl is good for this solution +1 , but C++ can be more fun :-)
Martin York
+3  A: 

Assuming you need to do this in C++ (awk would be easier) then you need to learn about iostreams.

#include <iostream>
#include <sstream>
#include <fstream>

int main()
{
  std::ifstream input("file.txt");
  std::stringstream sstr;
  std::string line;

  while(getline(input,line)) {
     if (line.length() > 4) {
         std::cout << line.substr(4);  // Print from the 4th character to the end.
     }
  }
}

By default getline reads the input until it gets end of line. You can also have it read input until it gets a specific character, eg space or comma with getline(stream,string,delimiter). In this way you can read a line a word at a time and process the individual values.

ps. When is SO going to get intellisense?

Martin Beckett
Won't "input >> sstr" give you one field, rather than one line?
David Thornley
oops, sorry was modifing an example using getline
Martin Beckett
Much better, and now a very good answer.
David Thornley
Why 999? If you do not specify the last parameter it is the same as std::string::npos which is the equivalent of the rest of the line.
Martin York
But it is more fun to compilicate it and use std::copy ;-)
Martin York
Also note if 4 > line.size() this will throw an exception.
Martin York
999 is a habit from a bad string implementation that didn't have a default value!
Martin Beckett
should use: std::min(4,line.size())
Martin York
I'm guessing the OP only wants to print the lines with all the fields. Posting example code is always a balance between being easily readable and correct, that why I didn't do any exception handling.
Martin Beckett
+1  A: 

I prefer the answer by 'mgb' above.
But just for fun:

#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>

struct Line
{
    std::string line;
};
std::ostream& operator<<(std::ostream& str,Line const& data) {return str << data.line << "\n";}
std::istream& operator>>(std::istream& str,Line&       data) {return std::getline(str,data.line);}

template<std::string::size_type Start>
struct ShortLine: public Line
{
    ShortLine(Line const& value)
    {
        // Note you need to check Start is in the correct rangs.
        line    = value.line.substr(std::min(Start,value.line.size()));
    }
};

int main()
{
    std::fstream    file("Plop");

    std::copy(  std::istream_iterator<Line>(file),
                std::istream_iterator<Line>(),
                std::ostream_iterator<ShortLine<4> >(std::cout)
            );

}
Martin York