Nov 19, 2012 at 10:53am Nov 19, 2012 at 10:53am UTC
Hey,
I tried to search a string in a txt file, and write the found ones into another file.
but i have a problem, it seems that my code doesnt enter the while loop, becaus the cout<<"test" doesnt work.. :/
could somebody explain me where the problem is?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int counter = 0;
string text;
ifstream input;
ofstream output;
void search(string text){
string foundtxt;
cout<<"search for: " ;
cin>> foundtxt;
input.open("story.txt" );
if (!input.is_open())
{
cout << "not open" ;
}else {
while (!input.eof()){
cout<<"test" ;
getline(input, text);
cout<<"test" ;
if (text.find(foundtxt) != string::npos){
cout<<"hello" ;
output.open("zwischenspeicher.txt" );
output<<counter<<endl<<text;
text = " " ;
counter++;
}
else {
text = " " ;
counter++;
}
}return ;}
}
int main(){
input.open("story.txt" , ios_base::in);
if (!input.is_open())
{
cout << "file not open" ;
}
else
{
while (!input.eof())
{
getline(input, text);
}
}
input.close();
search(text);
}
Last edited on Nov 19, 2012 at 10:53am Nov 19, 2012 at 10:53am UTC
Nov 19, 2012 at 12:41pm Nov 19, 2012 at 12:41pm UTC
The problem is on line 21: you get only the first word (i.e. "Gutenberg") since >> stops at the first space. use getline()
for the whole line