using vectors to parse text delimited files in C++
-
I want to use vectors in C++ on a linux box to parse a tab delimited text file and parse something in the second column. In perl I could do something like this: In perl: #!/usr/local/bin/perl while(<>) { chomp; ($first_col, $second_col, $third_col)=split(/\t/,$_); if ($second_col =~ m/$target/g){ print "$second_col\n"; } } In C++ I have the following 2 D code, can someone modify it to parse out text in the column 2 using vectors? Thanks for everyones help in advance.... #include #include #include #include #include using namespace std; int main() { ifstream ff( "test.txt", ios::in ); if ( !ff.is_open() ) return -1; string name; vector names; while( !ff.eof() ) { ff >> name; if (!ff.eof()) names.push_back( name ); } copy (names.begin(), names.end(), ostream_iterator(cout,"\n")); return 0; }
-
I want to use vectors in C++ on a linux box to parse a tab delimited text file and parse something in the second column. In perl I could do something like this: In perl: #!/usr/local/bin/perl while(<>) { chomp; ($first_col, $second_col, $third_col)=split(/\t/,$_); if ($second_col =~ m/$target/g){ print "$second_col\n"; } } In C++ I have the following 2 D code, can someone modify it to parse out text in the column 2 using vectors? Thanks for everyones help in advance.... #include #include #include #include #include using namespace std; int main() { ifstream ff( "test.txt", ios::in ); if ( !ff.is_open() ) return -1; string name; vector names; while( !ff.eof() ) { ff >> name; if (!ff.eof()) names.push_back( name ); } copy (names.begin(), names.end(), ostream_iterator(cout,"\n")); return 0; }
Easiest way is to read each line into a string first, then use an
istringstream
to get the second token in that string:#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>using namespace std;
int main()
{
ifstream ff( "test.txt", ios::in );
if ( !ff.is_open() )
return -1;string name;
vector<string> names;while( !ff.eof() )
{
std::string line;
std::getline(ff, line);
std::istringstream iss(line);std::string first, second; iss >> first >> second; if (iss) names.push\_back(second);
}
copy (names.begin(), names.end(), ostream_iterator<string>(cout,"\n"));
return 0;
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Easiest way is to read each line into a string first, then use an
istringstream
to get the second token in that string:#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>using namespace std;
int main()
{
ifstream ff( "test.txt", ios::in );
if ( !ff.is_open() )
return -1;string name;
vector<string> names;while( !ff.eof() )
{
std::string line;
std::getline(ff, line);
std::istringstream iss(line);std::string first, second; iss >> first >> second; if (iss) names.push\_back(second);
}
copy (names.begin(), names.end(), ostream_iterator<string>(cout,"\n"));
return 0;
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p