file i/o

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
mhh, now i have:

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
70
71
72
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>

using namespace std;

double counter = 0;
int found = 0;
string text;


ifstream input;
ofstream output;



void search(string text){
     string foundtxt;
     cout<<"Gib ein Wort ein nach dem gesucht werden soll: ";
     cin>> foundtxt;
     
     
     input.open("C:/Users/Sebastian/Desktop/uebung 7/aufgabe 2/story.txt");
     if (!input.is_open())
     {
    cout << "Datei wurde nicht geoeffnet";
    }else{
          
     while(!input.eof()){
                        
                        getline(input, text);
                        
                        if(text.find(foundtxt) != string::npos){
                                               output.open("C:/Users/Sebastian/Desktop/uebung 7/aufgabe 2/zwischenspeicher.txt", ios::app);
                                               
                                               output<<counter<<endl<<text<<endl;
                                               cout<<"Ausdruck gefunden in Zeile: "<<counter<<" Zeileninhalt: "<<endl<<text<<endl;
                                               cout<<endl;
                                               counter++;
                                               output.close();
                                               found++;
                                               }
                        else{
                             
                             counter++;
                        }
                        
                        
                        
                        
     
     }cout<<"Gefundene Stellen: "<<found<<endl;}
    return;
     }
     
     
     
     
int main(){
    
    input.open("C:/Users/Sebastian/Desktop/uebung 7/aufgabe 2/story.txt", ios_base::in);

if (!input.is_open())
  {
    cout << "Datei wurde nicht geoeffnet";
  }

      input.close();
      search(text);
    }


only problem now:

if i search for i.e. "Gutenberg eBook" i get also lines where only Gutenberg or only eBook where written...

how can i change != string::npos that i only get the exact ones?
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
thanks, it works :D
Topic archived. No new replies allowed.