954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

find string in txt file

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

FTProtocol
Junior Poster
102 posts since May 2008
Reputation Points: -14
Solved Threads: 1
 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

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

	if(RapidShit.is_open())
	{
		while (! RapidShit.eof() )
		{
			getline(RapidShit, FuckNipples);
			if(FuckNipples == "asdasdasd")
			{
				// how to skip 2 lines and get the other 2 into the buffers
			}			
		}
	}
	cin.get();
	return 0;
}
FTProtocol
Junior Poster
102 posts since May 2008
Reputation Points: -14
Solved Threads: 1
 

>>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;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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 FuckNipples;

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

			if(FuckNipples == "WORD")
			{
				getline(RapidShit, FuckNipples);
				cout << FuckNipples << endl;
				getline(RapidShit, FuckNipples); // First buffer i need
				cout << FuckNipples << endl;
				getline(RapidShit, FuckNipples); // Second
				cout << FuckNipples << 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 :P

FTProtocol
Junior Poster
102 posts since May 2008
Reputation Points: -14
Solved Threads: 1
 

>>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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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 FuckNipples;

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


Probably far too long but it works.

FTProtocol
Junior Poster
102 posts since May 2008
Reputation Points: -14
Solved Threads: 1
 

>>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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

lol im not lucky it works 100% :P

FTProtocol
Junior Poster
102 posts since May 2008
Reputation Points: -14
Solved Threads: 1
 
lol im not lucky it works 100% :P

Then what's the point of this thread, anymore?

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 
Amazingly it compiles without error


Amazingly, this compiles without error (for me) :icon_eek:

So I'm not too surprised that his compiled x)

char ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ******************************************************************************** a;
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

AFAIK there is no limit to the number of stars.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You