Hi,

I search everywhere and i didn't found anything good.

Do you know how to search a string inside a .txt file ??

Like, this is a text file:
--------------------------
i love mc donalds food
i hate my brother
i want a answer
a good answer
--------------------------
How can i search by one of this phrases and output it in the screen with a c++ program !?

Edited 6 Years Ago by ^Y^ nobody ^Y^: n/a

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
	string str;
	string str2 = "hate";
	int i;
	ifstream file ("file.txt");
	for (int index=1; index<=4; index++) {
		getline (file, str);
		i = str.find(str2);
		cout << "Line " << index << ": ";
		if(i != string::npos)
			cout << "Word found\n";
		else cout << "Word not  found\n";
	}
	cin.get();
	file.close();
}

Try this. You can change searching word for by changing str2 value. If you want to check more lines in file just change 10 line with for example this:

for (int index=1; index<=8; index++) {

This will check 8 lines. Hope you undestood ;)

Edited 6 Years Ago by fire_: n/a

I'm sure the OP will thank you for doing his homework for him, even though it's not 100% correct. But I'll give you a B+ for good effort.

I'm sure the OP will thank you for doing his homework for him, even though it's not 100% correct. But I'll give you a B+ for good effort.

>.> i didn't knew that this was his homework... and why is this not correct? for me it's working perfect

I'm sure the OP will thank you for doing his homework for him, even though it's not 100% correct. But I'll give you a B+ for good effort.

Homework ??

This is not my homework, i'm not learning c++ with anybody.

I learn alone, at home, with internet and guys like _fire...

--------------------------------------------

How can i set the file where the program is going to search the string ??

thank you

Edited 6 Years Ago by ^Y^ nobody ^Y^: n/a

In this line

ifstream file ("file.txt");

replace file.txt with any other file. Just don't forget to put file name in ""

In this line

ifstream file ("file.txt");

replace file.txt with any other file. Just don't forget to put file name in ""

lol

that's really obvious...

i gotta problem.

i say to search "hate";

the file is:

-------------
love
adcadc
dcadca
cdadcad
hate

---------

( 5 lines )

but i say him to search in 10 lines. He says
--------------
not found
not found
not found
not found
found
found
found
found
found
found
----------------


Weird...

That's because there is no lines after line 5. That means program will search in line 5 again and again...

Add these lines:

string str3;

After line 7 and these

if (str == str3)
	break;
str3 = str;

after line 11. Sorry i'm a beginner and that the best what i could work out.

Edited 6 Years Ago by fire_: n/a

Add these lines:

string str3;

After line 7 and these

if (str == str3)
	break;
str3 = str;

after line 11. Sorry i'm a beginner and that the best what i could work out.

Begginer !?

very good job.. another thing

can i add info to a file ??

like i have a file with:
----------------
a
b
c
d
e
f
------------------

can i add the rest ??
ADD, not rewrite all !


P.S. do you have msn ?

You need to use append openmode if you want to add.

ofstream file ("file.txt", ios::app)

Seperate openmodes with |

No i don't have MSN messenger if you were talking about it.

Edited 6 Years Ago by fire_: n/a

This article has been dead for over six months. Start a new discussion instead.