0

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

2
  • What is the output format spec? Whitespace, spaces, tabs? Commented Jul 30, 2009 at 20:02
  • This sounds a lot like a homework assignment. Commented Jul 30, 2009 at 20:52

4 Answers 4

3

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?

Sign up to request clarification or add additional context in comments.

8 Comments

Won't "input >> sstr" give you one field, rather than one line?
oops, sorry was modifing an example using getline
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.
But it is more fun to compilicate it and use std::copy ;-)
Also note if 4 > line.size() this will throw an exception.
|
1

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)

1 Comment

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

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.

1 Comment

I don't think the OP knows how to do the basic input/output though.
1

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) ); } 

Comments