**hello every body i am trying to filter alog file witch is save as txt file on my computer so i want to filter the log file depend on a particular word and this word apper in 3 column but the column i want is th 3rd one for example ,now i know that i must use the split function but i cant figure how i must use it i will atatch my code mybe you can understand it and help me to make it work **

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int main()
{ 
string temp[100];  // temporary  array to store strings for each line  
int column=0; 
ifstream file1;
ofstream file2 ;
char read[1000];

file1.open("d:\h1.txt");
file2.open("d:\learningsplit.txt");
if(!file1)
{
cout<<endl<<endl<<endl;
cout<<"              This file1 CANNOT be opened .";cout<<endl;

}


if(!file2)
{
cout<<endl<<endl<<endl;
cout<<"              This file2 CANNOT be opened .";cout<<endl;
}




int line=1;

while(!file1.eof())
{
string x;
 getline(file1,x);  // Get the content of the line from the read file and put it inside "read" array.


   string s=x;   // the previous is a sample string, it is a line from log file
// start of the code to split and store in array each column is stored at location column-1 in the temp array
    string::size_type prev_pos = 0, pos = 0;
    while( (pos = s.find('\t', pos)) != std::string::npos )
    {   column++;
        string substring( s.substr(prev_pos, pos-prev_pos) );
         temp[column-1]=substring;
         prev_pos = ++pos;
    }
    string substring( s.substr(prev_pos, pos-prev_pos) ); // Last word
    temp[column]=substring;

     // to test the code 
     for (int i=0; i<column; i++)
   cout<<temp[i]<<'\t'<<i+1<<endl;
// end of split ioperation for a single line
//NOW YOU HAVE YOUR ENTIRE ROW STORED SEPARATELY IN AN ARRAY INDIXED BY COLUMN NUMBER-1
//YOU CAN ACCESS EACH SRTING SEPARATELY AND USE IT IN COMPARISON FOR FILTERING.
// printing array of contents , THIS IS JUST A TEST
}

**thes is the word i want filter the file according to it //line=temp[i].find("hebron");**
// if(line>0)
// {file2<<x<<"\n"; // Write the contents of the line to the output file.
//


file2.close();  // Close the output file to save the changes.

cout<<endl<<endl;
cout<<"             Reading is done successfully ! "<<endl;
getchar();
return 0;
}

please help me very soon i need the helpe befor this sundy :)

Split the line using a std::istringstream ?

#include <vector>
#include <string>
#include <sstream>

std::vector< std::string > split_into_ws_separated_columns( 
                                      const std::string& line_from_file )
{
    std::vector< std::string > columns ;
    std::istringstream stm(line_from_file) ;
    std::string col ;
    while( stm >> col ) columns.push_back(col) ;
    return columns ;
}
commented: Much cleaner approach. +8

hello vijayan thanks for your fast reply
but can you explain more :) where can i put thes code?

where can i put thes code?

Anywhere. For instance, you could call the function split_into_ws_separated_columns() from main()

int main()
{
    std::ifstream file( "d:\\h1.txt" ) ;
    std::string line ;
    while( std::getline( file, line ) )
    {
       std::cout << "line: " << line << '\n' ;
       std::vector< std::string > cols = split_into_ws_separated_columns(line) ;
       for( std::size_t i = 0 ; i < cols.size() ; ++i )
        std::cout << "\tcolumn #" << i << ": " << cols[i] << '\n' ;
    }
}

thanks alot my dear that was very helpfull :)

it give me a black screen with no out but

#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

std::vector< std::string > split_into_ws_separated_columns(const std::string& line_from_file )
{ std::vector< std::string > columns ;
  std::istringstream stm(line_from_file) ;
   std::string col ;
     while( stm >> col ) 
     columns.push_back(col) ;
      return columns ;}

      int main()
      { 
          std::ifstream file("d:\hebro4.w3c") ;
       std::string line ;
         while(std::getline( file, line ) )
         {

         std::cout << "line: " << line << '\n' ;
          std::vector< std::string > cols = split_into_ws_separated_columns(line) ;
              for( std::size_t i = 0 ; i < cols.size() ; ++i )
               std::cout << "\tcolumn #" << i << ": " << cols[i] << '\n' ;
               }

               getchar();
               return 0;
                              }

std::ifstream file("d:\hebro4.w3c") ;// * ** * error in path to file

std::ifstream file( R"(d:\hebro4.w3c)" ) ; // raw literal string
or std::ifstream file( "d:\\hebro4.w3c" ) ; // escape sequence
or std::ifstream file( "d:/hebro4.w3c" ) ; // posix path

it is not work but thanks any way :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.