RSS Forums RSS

find string in txt file

Please support our C++ advertiser: Programming Forums
Reply
Posts: 91
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

find string in txt file

  #1  
Dec 2nd, 2008
ok so im trying to find a certain string in a txt file and once its found it needs to skip that line, the next line then add the following 2 lines into their own buffers.

EG:

Example ofomgwtfcantfindit kthx

Woot
Bang

asdasd
'asdasd
asdasd


so it finds the string "ofomgwtfcantfindit then it would skip that line, the blank line under it and add Woot to buffer1 and Bang to buffer2.

anyone have any ideas
AddThis Social Bookmark Button
Reply With Quote  
Posts: 91
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: find string in txt file

  #2  
Dec 2nd, 2008
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream RapidShit ("C:\\Rapid.txt");
	string ****Nipples;

	if(RapidShit.is_open())
	{
		while (! RapidShit.eof() )
		{
			getline(RapidShit, ****Nipples);
			if(****Nipples == "asdasdasd")
			{
				// how to skip 2 lines and get the other 2 into the buffers
			}			
		}
	}
	cin.get();
	return 0;
}
Reply With Quote  
Posts: 13,871
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1231
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: find string in txt file

  #3  
Dec 2nd, 2008
>>string ****Nipples;

OMG what in hell are you trying to do here??? If you are trying to code an array of string's then its string Nipples[4];
But the reading part should look like this: (I hope you change the variable names before turning in the assignment because your teacher may not be that humerious.)
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    ifstream RapidShit ("C:\\Rapid.txt");
    vector<string> Nipples; // an array of strings
    string line; // one line in the file

    if(RapidShit.is_open())
    {
        while (getline(RapidShit, line) )
        {
           Nipples.push_back(line); // add to the array
        }
        // now you have the entire file in memory.  Search each line
        // for the desired entry.
    }
    cin.get();
    return 0;
}
Last edited by Ancient Dragon : Dec 2nd, 2008 at 7:37 pm.
Reply With Quote  
Posts: 91
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: find string in txt file

  #4  
Dec 2nd, 2008
Lol, this isnt for an assignment. I code because i can not because i have to but this is what i have so far.

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

char szUser[50], szPass[50];
bool bFound = false;

int main()
{
	ifstream RapidShit ("C:\\Rapid.txt");
	string ****Nipples;

	if(RapidShit.is_open())
	{
		while (!bFound)
		{
			getline(RapidShit, ****Nipples);
			cout << ****Nipples << endl;

			if(****Nipples == "WORD")
			{
				getline(RapidShit, ****Nipples);
				cout << ****Nipples << endl;
				getline(RapidShit, ****Nipples); // First buffer i need
				cout << ****Nipples << endl;
				getline(RapidShit, ****Nipples); // Second
				cout << ****Nipples << endl;
				bFound = true;
			}			
		}
	}
	cin.get();
	return 0;
}

Just wonder how i can store those First buffer line and Second buffer line in char szBuffer1[50], szBuffer2[50];

Thanks
Reply With Quote  
Posts: 13,871
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1231
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: find string in txt file

  #5  
Dec 2nd, 2008
>>string ****Nipples;
Forget about doing that! It won't work. That is not four strings, but a pointer to pointer to pointer to pointer of type string. Amazingly it compiles without error, but that doesn't mean it will work, which it won't.
Last edited by Ancient Dragon : Dec 2nd, 2008 at 9:18 pm.
Reply With Quote  
Posts: 91
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: find string in txt file

  #6  
Dec 2nd, 2008
oh it works

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

char szUser[50], szPass[50];
bool bFound = false;

void GetUser(string szInput)
{
	const char *szUserBuffer;

	std::string szTemp = szInput;
	szUserBuffer = szTemp.c_str();
	sprintf(szUser, "%s", szUserBuffer);

	for (int i = strlen(szUser) + 1; i > 0; i--)
	{
        if( szUser[i] == ':')
        {
            strcpy(szUser, &szUser[i + 2]);
            break;
        }
	}
}

void GetPassword(string szInput)
{
	const char *szPassBuffer;

	std::string szTemp = szInput;
	szPassBuffer = szTemp.c_str();
	sprintf(szPass, "%s", szPassBuffer);

	for (int t = strlen(szPass) + 1; t > 0; t--)
	{
        if( szPass[t] == ':')
        {
            strcpy(szPass, &szPass[t + 2]);
            break;
        }
	}
}

int main()
{
	ifstream RapidShit ("C:\\Rapid.txt");
	string ****Nipples;

	if(RapidShit.is_open())
	{
		while (!bFound)
		{
			getline(RapidShit, ****Nipples);
			if(****Nipples == "https://ssl.rapidshare.com")
			{
				getline(RapidShit, ****Nipples);
				getline(RapidShit, ****Nipples);
				GetUser(****Nipples);
				getline(RapidShit, ****Nipples);
				GetPassword(****Nipples);
				bFound = true;
			}			
		}
	}
	cin.get();
	return 0;
}

Probably far too long but it works.
Reply With Quote  
Posts: 13,871
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1231
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: find string in txt file

  #7  
Dec 2nd, 2008
>>Probably far too long but it works.
Only because you were very lucky in your tests.

>>getline(RapidShit, ****Nipples);
What does that pointer Nipples point to? Nowhere -- just some random number because Nipples was never initialized.
Reply With Quote  
Posts: 91
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: find string in txt file

  #8  
Dec 4th, 2008
lol im not lucky it works 100%
Reply With Quote  
Posts: 717
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 80
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Master Poster

Re: find string in txt file

  #9  
Dec 4th, 2008
Originally Posted by FTProtocol View Post
lol im not lucky it works 100%


Then what's the point of this thread, anymore?
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote  
Posts: 999
Reputation: William Hemsworth is a splendid one to behold William Hemsworth is a splendid one to behold William Hemsworth is a splendid one to behold William Hemsworth is a splendid one to behold William Hemsworth is a splendid one to behold William Hemsworth is a splendid one to behold William Hemsworth is a splendid one to behold William Hemsworth is a splendid one to behold 
Solved Threads: 94
Sponsor
William Hemsworth's Avatar
William Hemsworth William Hemsworth is offline Offline
Posting Shark

Re: find string in txt file

  #10  
Dec 4th, 2008
Originally Posted by Ancient Dragon
Amazingly it compiles without error
Amazingly, this compiles without error (for me)

So I'm not too surprised that his compiled x)
  1. char ********************************************************************************
  2. ********************************************************************************
  3. ********************************************************************************
  4. ********************************************************************************
  5. ********************************************************************************
  6. ********************************************************************************
  7. ********************************************************************************
  8. ********************************************************************************
  9. ********************************************************************************
  10. ********************************************************************************
  11. ******************************************************************************** a;
Last edited by William Hemsworth : Dec 4th, 2008 at 12:13 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 815 | Replies: 10 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:45 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC