Solved

Search for a tab delimiter in a line

Posted on 2004-04-24
4
295 Views
Last Modified: 2010-04-01
I posted a similar question earlier, then decided I would like to search for the first tab then extract that value after that.

For example, this is the file (but with several more lines):

Test     1        A    N    testtt     12345
Test     1        A    N    testtt     12345
Test     1        A    N    testtt     12345
Test    21       A    N    testtt     12345
Test    41       A    N    testtt     12345
Test    21       A    N    testtt     12345

I need to search the 2nd column within this file.  I am reading it using getline, then I was going to use substr and specify the location, but since the 2nd field's position varies, I can't do it that way.

I was thinking about searching for the 1st tab within that file.  If found, then I could set the position to read the 2nd column (which is after the 1st tab).  
Is this possible without having to use a C function like strtok?
0
Comment
Question by:jewee
  • 2
4 Comments
 
LVL 11

Assisted Solution

by:avizit
avizit earned 50 total points
ID: 10910388
Is the file produced by a program or edited by human ?

if  it's program it's okay to search for "\t"  but if its human edited then it might be sequence of space character and not a '\t'
so you need to take care of that .

( ps :

1)if you are not under compulsion to do it in C++ , script languages like perl etc may give you less painful solutions )
2) if you have control over the format .. i guess a more pragmatic format would be using a delimiter like ':' ( unless your data can contain a ':'  as you dont have the confusion of whether that was a tab or 4 spaces or was it 5 )


/abhijit/
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 200 total points
ID: 10910649
You can use scanf or fscanf to pull the data out of a line.
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 200 total points
ID: 10910667
Instead of using getline function, you can just use operator >> functions for each field.
Example:
int main(int argc, char* argv[])
{
      ifstream file;
      file.open("c:\\test2.txt");

      char Field1[33];
      int Field2;
      char Field3;
      char Field4;
      char Field5[33];
      int Field6;
      do
      {
            file >> Field1 >> Field2 >> Field3 >> Field4 >> Field5 >> Field6;
            if (!file.eof())
                  cout << Field1 << ", " << Field2 << ", " << Field3 << ", " << Field4 << ", " << Field5 << ", " << Field6 << endl;
      }while(!file.eof());
      system("pause");
      return 0;
}

The above code will fetch the data for each line, and will do so even if the data is seperated by tabs or spaces.
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 250 total points
ID: 10910896
You can use getline on istringstream with the '\t' delimiter, if the tab stops are guaranteed to be hard.

i.e.:
--------8<--------
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>

int main()
{
        using std::string;
        std::ifstream fin("tab.txt");   // Your input file
        string line;
        while (getline(fin,line)) {
                std::istringstream istr(line);
                std::vector<string> tokenList(6);
                for (int i = 0;i < tokenList.size();i++)
                        if (!getline(istr,tokenList[i],'\t'))
                                break;
                if (tokenList[1] == "21") // Looking for 2nd field with "21"
                        std::cout << "Value for a 21 is \"" << tokenList[5] << "\"\n";
        }
}
--------8<--------
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now