Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using vectors to parse text delimited files in C++

using vectors to parse text delimited files in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++perliosgraphicslinux
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    meixiang6
    wrote on last edited by
    #1

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

    S 1 Reply Last reply
    0
    • M meixiang6

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

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • S Stuart Dootson

        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

        M Offline
        M Offline
        meixiang6
        wrote on last edited by
        #3

        Thanks stuart, it works nicely.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups