0

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 by ^Y^ nobody ^Y^: n/a

3
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by fire_
-1
#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 by fire_: n/a

0

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.

0

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

0

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 by ^Y^ nobody ^Y^: n/a

0

In this line

ifstream file ("file.txt");

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

0

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

0

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

0

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 by fire_: n/a

0

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 ?

0

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 by fire_: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.