search word in file: i get different output like i want

Pages: 12
closed account (iN8poG1T)
hello everyone, i know this is very messy but i have a problem searching a word. i want to search my text file

user input : part

output should only be "part"

but what i get here is the output also shows the word "partner" which is also a part of "part" i dont want it to be like that, i want "part" only. how should i change my code here?

ALSO : how should i make it not case sensitive? if i enter Part, it still show"part"

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;



int main()
{
    ifstream fileInput;
    int offset;
    int line1=0, line2=0, line3=0, line4=0, line5=0, line6=0, line7=0, line8=0, line9=0, line10=0;
    string line;
    string word;
    char option;


    cout<<"Enter the word you would like to search : ";
    cin>>word;

    {


        fileInput.open("1.txt");

        {

            if(fileInput.is_open())
            {
                while(!fileInput.eof())
                {
                    getline(fileInput, line);
                    line1++; //increments line number from default value 0 to display line number correctly
                    if ((offset = line.find(word, 0)) != string::npos)

                    {
                        cout<<endl;
                        cout<< "Line Number :"<<line1<<endl;
                        cout <<"File name   : 1.txt"<<endl;
                        cout<< "Sentences   :"<<line<<endl;
                    }


                }
                fileInput.close();
                fileInput.clear();
            }
             else
                {
                    cout<<"Error";
                }

            fileInput.open("2.txt");

            if(fileInput.is_open())
            {
                while(!fileInput.eof())
                {
                    getline(fileInput, line);
                    line2++; //increments line number from default value 0 to display line number correctly
                    if ((offset = line.find(word, 0)) != string::npos)

                    {
                        cout<<endl;
                        cout<< "Line Number :"<<line2<<endl;
                        cout <<"File name   : 2.txt"<<endl;
                        cout<< "Sentences   :"<<line<<endl;
                     }
                }
                fileInput.close();
                fileInput.clear();
            }


            fileInput.open("3.txt");

            if(fileInput.is_open())
            {
                while(!fileInput.eof())
                {
                    getline(fileInput, line);
                    line3++; //increments line number from default value 0 to display line number correctly
                    if ((offset = line.find(word, 0)) != string::npos)

                    {
                        cout<<endl;
                        cout<< "Line Number :"<<line3<<endl;
                        cout <<"File name   :3.txt"<<endl;
                        cout<< "Sentences   :"<<line<<endl;
                    }
                }
                fileInput.close();
                fileInput.clear();
            }



            fileInput.open("4.txt");

            if(fileInput.is_open())
            {
                while(!fileInput.eof())
                {
                    getline(fileInput, line);
                    line4++; //increments line number from default value 0 to display line number correctly
                    if ((offset = line.find(word, 0)) != string::npos)

                    {
                        cout<<endl;
                        cout<< "Line Number :"<<line4<<endl;
                        cout <<"File name   :4.txt"<<endl;
                        cout<< "Sentences   :"<<line<<endl;
                    }
                }
                fileInput.close();
                fileInput.clear();
            }


            fileInput.open("5.txt");

            if(fileInput.is_open())
            {
                while(!fileInput.eof())
                {
                    getline(fileInput, line);
                    line5++; //increments line number from default value 0 to display line number correctly
                    if ((offset = line.find(word, 0)) != string::npos)

                    {
                        cout<<endl;
                        cout<< "Line Number :"<<line5<<endl;
                        cout <<"File name   :5.txt"<<endl;
                        cout<< "Sentences   :"<<line<<endl;
                    }
                }
                fileInput.close();
                fileInput.clear();
            }


            fileInput.open("6.txt");

            if(fileInput.is_open())
            {
                while(!fileInput.eof())
                {
                    getline(fileInput, line);
                    line6++; //increments line number from default value 0 to display line number correctly
                    if ((offset = line.find(word, 0)) != string::npos)

                    {
                        cout<<endl;
                        cout<< "Line Number :"<<line6<<endl;
                        cout <<"File name   :6.txt"<<endl;
                        cout<< "Sentences   :"<<line<<endl;
                    }
                }
                fileInput.close();
                fileInput.clear();
            }

            fileInput.open("7.txt");

            if(fileInput.is_open())
            {
                while(!fileInput.eof())
                {
                    getline(fileInput, line);
                    line7++; //increments line number from default value 0 to display line number correctly
                    if ((offset = line.find(word, 0)) != string::npos)

                    {
                        cout<<endl;
                        cout<< "Line Number :"<<line7<<endl;
                        cout <<"File name   :7.txt"<<endl;
                        cout<< "Sentences   :"<<line<<endl;
                    }
                }
                fileInput.close();
                fileInput.clear();
            }


        }


        cout<<endl<<endl;
        cout<<"Continue? ";
        cin>>option;

        if (option=='y' || 'Y')
        {
            main();
        }

        else

        {
            return 0;
        }



    }

}



enter search word : part
line number : 1
file name : 1.txt
sentences :this is part 1

line number : 24
file name : 2.txt
sentences :partner with bbc


as you can see, i dont want the partner etc i only want part. also i need to search each file
Last edited on
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
#include <iostream>
#include<fstream>
#include<string>
using namespace std;

int main() {
	char * p=new char ();
	char *word=new char();
	cout << "Enter word you want to find" << endl;
	cin >> word;
	ifstream f("Text.txt");
	if (!f.good())
	{
		cout << "File did not open";
		f.close();
	}
	while (!f.eof())
	{
		f >> p;
		if (strcmp(p, word) == 0)
			cout << p << endl;

	}
	f.close();
delete p;
delete word;
}
Last edited on
closed account (iN8poG1T)
in which part should i change? also i need to search each text file, and the user doesnt need to input the file name

Here the user inputs only the word it wish to find.In your case you said it is part.But if you want you can just do something like this
1
2
const char *p="part"// it can not be changed further
char *word=new char();//this stays like it is 

So if you are searching multiple text files ypou mast include this in every one of it
1
2
3
4
5
6
7
8
while (!f.eof())//f is yours fileInput
	{
		f >> p;
		if (strcmp(p, word) == 0)
			cout << p << endl;
}
//you can also add this to know where is find in which file
//cout<<"It is find in file(name of file)<<endl; 

And also you can check every file if it has opened corectly .I use good() for that matter.

Last edited on
closed account (iN8poG1T)
I got an error for the line 26. is it like this?

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
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main ()
{


    const char *p=new char();
    char *word=new char();



    cout<<"Enter the word you would like to search : ";
    cin>>word;
         ifstream fileInput("1.txt");
         if(fileInput.good())
         {
             cout <<"error open";
             fileInput.close();
         }
         while(!fileInput.eof())
         {
             fileInput>>p;
             if(strcmp(p, word) == 0)
               
              cout<<"File name : 1.txt"<<p <<endl; 

         }
         fileInput.close();
       


         delete p;
         delete word;
}
Last edited on
Now for const char * you cant do const char *p=new char();
You can const char *p="part" in your case and you do not delete it because we did not allocate memory with new.Change that and the code will work i hope .It worked on my compilier.
Last edited on
closed account (iN8poG1T)
like this? it doesn't work. when i try to enter the search word, it show error open

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
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main ()
{


    char *p;
    char *word=new char();



    cout<<"Enter the word you would like to search : ";
    cin>>word;
         ifstream fileInput("1.txt");
         if(fileInput.good())
         {
             cout <<"error open";
             fileInput.close();
         }
         while(!fileInput.eof())
         {
             fileInput>>p;
             if(strcmp(p, word) == 0)

              cout<<"File name : 1.txt"<<p <<endl;

         }
         fileInput.close();



         delete word;
}
if(!fileInput.good())-! this means if it is different from good opening.And if you want a user to input a word than you mast make p and word char * because const char can not be changed.And if you want to have your own word than you enter it alone before checking.
USER ENTERS NAME
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	
	 char *p=new char();//THIS WORD ARE WE LOOKING FOR
	char *word = new char();



	cout << "Enter the word you would like to search : ";
	cin >> word;
	ifstream fileInput("1.txt");
	if (!fileInput.good())
	{
		cout << "error open";
		fileInput.close();
	}
	while (!fileInput.eof())
	{
		fileInput >> p;
		if (strcmp(p, word) == 0)

			cout << "File name : 1.txt" << word<< endl;

	}
	


	
	fileInput.close();

}

WE ENTER NAME
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	
	 char *p=new char();//THIS WORD ARE WE LOOKING FOR
	char *word = "part";



	
	ifstream fileInput("1.txt");
	if (!fileInput.good())
	{
		cout << "error open";
		fileInput.close();
	}
	while (!fileInput.eof())
	{
		fileInput >> p;
		if (strcmp(p, word) == 0)

			cout << "File name : 1.txt" << word<< endl;

	}
	


	
	fileInput.close();
}

This program works.I removed delete p and
EDIT: Sorry ,i made a mistake not allocating memory for pointers.This are the two corrected versions:
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	
	 char *p=new char[20];//assuming that word has maximum 20characters
	char *word = new char[20];



	cout << "Enter the word you would like to search : ";
	cin >> word;
	ifstream fileInput("1.txt");
	if (!fileInput.good())
	{
		cout << "error open";
		fileInput.close();
	}
	while (!fileInput.eof())
	{
		fileInput >> p;
		if (strcmp(p, word) == 0)

			cout << "File name : 1.txt" << word<< endl;

	}
	


	
	fileInput.close();
delete p;
delete word;
return 0;

}

In this case we already have a word
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
int main()
{
	
	 char *p=new char[20];//assuming that word has maximum 20characters
	const *word = "part";



	
	ifstream fileInput("1.txt");
	if (!fileInput.good())
	{
		cout << "error open";
		fileInput.close();
	}
	while (!fileInput.eof())
	{
		fileInput >> p;
		if (strcmp(p, word) == 0)

			cout << "File name : 1.txt" << word<< endl;

	}
	


	
	fileInput.close();
delete p;
//delete word;because it is const we do not need this
return 0;
}
Last edited on
closed account (iN8poG1T)
yes i fixed it now i get the point but it still gives me error, can run but it didnt show anything. i put my text file in the same folder
Last edited on
Try this other updated codes i posted.Work on my Visual Studio 2017.
Last edited on
closed account (iN8poG1T)
Tried, but still same. it didnt read the file. the output shows nothing


Enter the word you would like to search : small



user only enter the word then it appear nothing
Last edited on
Put a breakpoint before return 0.Because there is no newline mine console puts
File name : 1.txtpart ;because I entered part
Last edited on
closed account (iN8poG1T)
i figure it out but now how should i make case insensitive? if there is a word Krill and user enter krill, it wont match because the upper case K, i want it to be case insensitive. any help? Also, in the code you give me, if the word is found, it only show the text file and the word itself, so how i want to output the whole line where the word is found?

Last edited on
You can see ASCII table
https://www.google.com/search?q=ascii+table&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjt7s7J77ngAhVpp4sKHfr9CZYQ_AUIDigB#imgrc=uTkeCfqmL0Fz1M:
So if you want only the first letter to be big than you can get the small letter like this
for examle 'a'-'A'=32.From this you get 'A'=32+'a'.So you can ask in if statment
 
if(strcmp(p,word)==0 || p[0]+32=word[0])

Here p[0] is first letter of the word extracted from text ,and word[0] is first letter of user input word .So if user inputs Krill for example not krill p and word are now equal but the second condition is true
False OR true is true because difference between small and bil letters is 32 or you do not need to remember just do 'a'-'A'=32 or 'b'-'B' and so on.


closed account (iN8poG1T)
Ok and how should i output the line number and the line that has the word?
My idea is to count '\n'+1 for the number of line.But for the line I am uncertain
You started out using a std::string, stick with the standard string.

Next functions should be your friend. The file read code should be in a function that can be called with the file name of the file you want to search and the word you want to search for.

You're looking for the presence of the word in the line so use getline() to get an entire line into a string, then use a stringstream to get each word from that line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void checkForWord(const std::string& inputFile, const std::string& search)
{
   // Open and check that the file opened correctly here.
...
   std::string word, line;
   while(getline(inputFile, line))
   {
       std::stringstream sin(line);
       while(sin >> word)
       {
           // If you want case insensitivity "transform" word into the desired case.
           if(word.length() == search.length() && word == search)
           {
                // You found a match so do whatever.
...


closed account (iN8poG1T)


I got error saying invalid initialization of reference type in line 45. ALSO, how to output the line number too? in this case, i got the line sentences, file name but not the line number


C:\Users\User\Downloads\Compressed\259194_Assignment.2\1to10.Text.Files\testing.cpp|45|error: invalid initialization of reference of type 'const string& {aka const std::__cxx11::basic_string<char>&}' from expression of type '<unresolved overloaded function type>'|


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
#include <iostream>
#include <cstring>
#include <sstream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>
#include <cctype>
#include <cstdlib>

using namespace std;

void checkForWord(const std::string& search)
{


    ifstream fileInput("1.txt");
	if (!fileInput.good())
	{
		cout << "error open";
		fileInput.close();
	}
	std::string word, line;
    while(getline(fileInput, line))
    {
    std::stringstream sin(line);
       while(sin >> word)
       {
            if(word.length() == search.length() && word == search)
           {
                cout<<endl;
                cout<< "Sentences:"<<line<<endl;
                cout <<"File name   :1.txt"<<endl;

           }
       }
    }
    fileInput.close();

}

int main ()
{
    checkForWord(search);
}


Last edited on
Well the first thing I notice is that you're #including a lot of unnecessary headers and that you are missing one of the necessary headers. You should be #including the following:

1
2
3
4
#include <iostream>
#include <string>
#include <sstream>
#include <fstream> 


Next in main your calling your function with a variable that has not been created or initialized.

Next in the function if the file fails to open you should not try to process the file, just return, or maybe change the function to return an error value.


Lastly, for now, you also need to pass the name of the file to process into the function.
closed account (iN8poG1T)
okay for example, i want to use this code, because it is already in what i want, line number, text file and also line of sentences. only the problem is case sensitive, i want it to make case insensitive which it can read bot lower and upper case. where should i change? please anyone help me :(((


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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;



int main()
{
    ifstream fileInput;
    int offset;
    int line1=0 ; //this another line is for my other file which i show in the orginal question
// ine2=0, line3=0, line4=0, line5=0, line6=0, line7=0, line8=0, line9=0, line10=0;
    string line;
    string word;
    char option;


    cout<<"Enter the word you would like to search : ";
    cin>>word;
        fileInput.open("1.txt");

        {

            if(fileInput.is_open())
            {
                while(!fileInput.eof())
                {
                    getline(fileInput, line);
                    line1++; //increments line number from default value 0 to display line number correctly
                    if ((offset = line.find(word, 0)) != string::npos)

                    {
                        cout<<endl;
                        cout<< "Line Number :"<<line1<<endl;
                        cout <<"File name   : 1.txt"<<endl;
                        cout<< "Sentences   :"<<line<<endl;
                    }


                }
                fileInput.close();
                fileInput.clear();
            }
        }
    }
Last edited on
Pages: 12