473,765 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

29 New Member
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
1 6838
rstiltskin
14 New Member
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

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

Similar topics

60
49188
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't be indexed/sorted. I don't want to load the entire file into physical memory, memory-mapped files are ok (and preferred). Speed/performance is a requirement -- the target is to locate the string in 10 seconds or less for a 100 MB file. The...
6
1960
by: thorpk | last post by:
I have an access database that i have added a pop up calendar to, the Table information for the Date Reported field is Date/Time format short date, input mask is 00/00/0000. i have created a combo box on the form and have bound the control to the Date Reported field. I have placed the calendar control on the form and set the Visible property to no. The following code has bee added to the Mouse Down Event of the Date
0
1517
by: fh217 | last post by:
Hi, Is there a way in Access to retrieve data in a specific area of a text file? The data ( a 6 character date in YYMMDD format ) always resides on the second line in positions 11 to 16. Thank you in advance for you help.
1
1738
by: ogo796 | last post by:
Hi everyone i want to search the text file,using pregmatch() but it seems like it dose't serch.maybe my serching pattern is not right.look at the following code. Myfile look like this -------------------------------- file : (49/06/01) simon 13; file : (68/05/01) John 17 ; i want to serch simon 13 on the text file. can anyone help me with pregmatch patterns.
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9833
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.