By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,903 Members | 2,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,903 IT Pros & Developers. It's quick & easy.

How to get position of a search in the text file?

P: 29
Hello, I have a bit of a problem here: I am trying to make the search find the positions of a string in the text file.

The .txt file is very large and the string comes up multiple times, so I need a way to delete part of the txt file up to the point I see the first occurence of the word

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  5. #include "sourcetxt.h"
  6.  
  7. using namespace std;
  8.  
  9. const string prntpath = "C:\\data.txt";
  10.  
  11. class txtfiles
  12. {
  13. private:
  14.     CURLcode crl;
  15.     int offset;
  16.     string url;
  17.     string frgdate;
  18.     string search;
  19.     ifstream infile;
  20. public:
  21.     txtfiles() //constructor for url
  22.     {
  23.         cout << "Enter the URL:";
  24.         cin >> url;
  25.  
  26.         crl = copy(url, prntpath);
  27.     };
  28.  
  29.     const int& organize()
  30.     {
  31.         infile.open(prntpath);
  32.  
  33.         if (infile.fail())
  34.         {
  35.             cout << "Error: Could Not Open Input File!";
  36.             return 1;
  37.         }
  38.  
  39.         cout << "Successfully Opened Input File." << endl;
  40.  
  41.         search = ", 200";
  42.         while (infile.good())
  43.         {
  44.             while (!infile.eof())
  45.             {
  46.                 getline(infile, frgdate);
  47.                 if ((offset = frgdate.find(search, 0)) != string::npos) 
  48.                 {
  49.                     cout << "found '" << search << "' @ offset " << offset << endl;
  50.                     return 0;
  51.                 }
  52.             }
  53.         }
  54.     };
  55. };
  56.  
I currently need help with 2 things:
1. Getting position of the string in the text file
2. Erasing the previous part of the text file

If you have experience, please help me.
Nov 8 '10 #1

✓ answered by rstiltskin

You can use infile.tellg() to find out the position of the beginning of each line -- use it as the last statement in the loop so you'll know the position before each call to getline. Then adjust that value by the position of your search string in the line.

http://www.cplusplus.com/reference/i...istream/tellg/

By the way, your code doesn't seem to allow for the possibility that the search string might overlap the end of a line. If that's a possibility, you can save the last few (search string length - 1) characters of each line to search with the next line.

Also, instead of
Expand|Select|Wrap|Line Numbers
  1.        while (infile.good())
  2.        {
  3.             while (!infile.eof())
  4.             {
  5.                 getline(infile, frgdate);
  6.                 // ...
  7.  
why not
Expand|Select|Wrap|Line Numbers
  1.        while (getline(infile, frgdate)
  2.        {
  3.                  // ...
  4.  

Share this Question
Share on Google+
1 Reply


P: 14
You can use infile.tellg() to find out the position of the beginning of each line -- use it as the last statement in the loop so you'll know the position before each call to getline. Then adjust that value by the position of your search string in the line.

http://www.cplusplus.com/reference/i...istream/tellg/

By the way, your code doesn't seem to allow for the possibility that the search string might overlap the end of a line. If that's a possibility, you can save the last few (search string length - 1) characters of each line to search with the next line.

Also, instead of
Expand|Select|Wrap|Line Numbers
  1.        while (infile.good())
  2.        {
  3.             while (!infile.eof())
  4.             {
  5.                 getline(infile, frgdate);
  6.                 // ...
  7.  
why not
Expand|Select|Wrap|Line Numbers
  1.        while (getline(infile, frgdate)
  2.        {
  3.                  // ...
  4.  
Nov 8 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.