Hi,

I'm trying to make a program that search strings in a file and output them (or others).
My problem is, how to output a file line !?

Here is my code...

string str;	
string str2 = "hate";	
int i;	ifstream file ("file.txt");	
for (int index=1; index<=10; index++) 
{		
getline (file, str);		
i = str.find(str2);		
cout << "Line " << index << ": ";		
if(i != string::npos)			
// print a line... How ??		
else 
// print a line...	 How ??
}	
cin.get();	
file.close();

I hope you understand and you have the answer... ;)

To output just a certain part of a string, you'll need to use std::string 's substr(size_t pos, size_t n) , which essentially extracts a part of the whole string, as specified by its parameters, where pos is the starting index and n represents the number of characters to extract after pos .

An example of its use would be:

if(i != string::npos)
    std::cout << str.substr(i, str2.length());
else
    std::cout << "Not found";
}

Hope this helped!

To output just a certain part of a string, you'll need to use std::string 's substr(size_t pos, size_t n) , which essentially extracts a part of the whole string, as specified by its parameters, where pos is the starting index and n represents the number of characters to extract after pos .

An example of its use would be:

if(i != string::npos)
    std::cout << str.substr(i, str2.length());
else
    std::cout << "Not found";
}

Hope this helped!

I'm sorry but i'm not very good in this..
Can you explain again? step by step..

Do that code print a line from a file ??
What line do that print ??

:S

I'm sorry, I think I may have misunderstood your question. My code doesn't print out the line, rather the word you looked for ("hate"). If you want to print out the lines containing the word, all you have to do is std::cout << str; . So then your code will look like this:

if(i != string::npos) // The word "hate" was found in line number index
    std::cout << str; // Just output the whole line directly through cout
else
    std::cout << "Not found in this line";
}

Is this what you wanted?

Edited 6 Years Ago by amrith92: n/a

I'm sorry, I think I may have misunderstood your question. My code doesn't print out the line, rather the word you looked for ("hate"). If you want to print out the lines containing the word, all you have to do is std::cout << str; . So then your code will look like this:

if(i != string::npos) // The word "hate" was found in line number index
    std::cout << str; // Just output the whole line directly through cout
else
    std::cout << "Not found in this line";
}

Is this what you wanted?

Perfect, thank you very much.

Do you have Msn Messenger ?

How do i select other line to print ?
like: if found in line 7, print 6, 7 and 8...
know that ?

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

>> How do i select other line to print ?
like: if found in line 7, print 6, 7 and 8...
know that ?

Well to do this in the simplest way possible, I recommend you load all the lines into a std::vector and then use another loop that iterates over its range to check for matches. Something like:

std::string search = "hate", str;
std::vector<std::string> lines; // Declare a vector of strings to store the lines


while(getline(file, str))
    lines.push_back(str); // Read a line from the file and store it in the vector

int sz = (int)lines.size();

for(int index = 0; index < sz; ++index) {
    if(lines[index].find(search) != std::string::npos) {
        if(index > 0) // There's no line before the first line, so we can't print it
            std::cout << "\nLine " << index-1 << ": " << lines[index-1];
        std::cout << "\nLine " << index << ": " << lines[index];
        if(index < sz-1) // There's no line after the last line, so we can't print it
            std::cout << "\nLine " << index+1 << ": " << lines[index+1];
    }
    else
        std::cout << "\nNot Found on line: " << index;
}

Note: You will have to make sure to include <vector> in the file

>> Do you have Msn Messenger ?

Yes, I do. Please check my profile page.

Edited 6 Years Ago by amrith92: n/a

How can i make the user input the "search" string ??

>> std::string search = "hate" ,str; <<


How to input this ??


thank you

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

How can i make the user input the "search" string ??

>> std::string search = "hate" ,str; <<


How to input this ??


thank you

you mean read from user input?
Use the std::cin for that.

#include <iostream>

int main(int argc,char **argv)
{
 std::string _input;
 std::cout <<"Please input some text(Enter to end):";
 std::cin >> input  ;
 
 /* Then you got what you need in the _input*/
 std::cout << _input << std::endl;

 return 0;
}

you mean read from user input?
Use the std::cin for that.

#include <iostream>

int main(int argc,char **argv)
{
 std::string _input;
 std::cout <<"Please input some text(Enter to end):";
 std::cin >> input  ;
 
 /* Then you got what you need in the _input*/
 std::cout << _input << std::endl;

 return 0;
}

OMG...
i think that should be one of the first things everyone learn...

my question is:

i have

std::string search = "something" ,str;

How can i make the user input the string ??

cin >> search; // don't work..
getline (cin, search); // don't work..

other solution !?

the problem is the " , str " ..

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

How can i make the user input the string ??

cin >> search; // don't work..
getline(cin, search); // don't work..

other solution !?

could you explain more?(however use std::cin if you use the `using namespace std;`
before hitting cout , cin.

could you explain more?(however use std::cin if you use the `using namespace std;`
before hitting cout , cin.

std::string search = "hate", str;

How can i input the search string ?

I don't know what the " , str; " means and how can i input the string with that..


Hope you understand :|

In the statement: std::string search = "hate", str; , search represents the keyword your looking for, whereas str is just used as a temporary variable to read and store a single line from the file. See this statement:

while(getline(file, str)) // str is used to store the newly-read line from the file
      lines.push_back(str); // Read a line from the file and store it in the vector

To allow the user to enter the search term, just do the following:

std::cout << "\nEnter search term: ";
std::getline(std::cin, search, '\n'); // Store what the user entered in search

Add those statements before the for -loop.

Edited 6 Years Ago by amrith92: n/a

In the statement: std::string search = "hate", str; , search represents the keyword your looking for, whereas str is just used as a temporary variable to read and store a single line from the file. See this statement:

while(getline(file, str)) // str is used to store the newly-read line from the file
      lines.push_back(str); // Read a line from the file and store it in the vector

To allow the user to enter the search term, just do the following:

std::cout << "\nEnter search term: ";
std::getline(std::cin, search, '\n'); // Store what the user entered in search

Add those statements before the for -loop.

string search; // The string to search
cout << "Enter search word: ";
getline(cin, search, '\n'); // ?????
vector<string> lines; // Declare a vector of strings to store the lines


while(getline(file, str))
    lines.push_back(str); // Read a line from the file and store it in the vector

int sz = (int)lines.size();

for(int index = 0; index < sz; ++index) {
    if(lines[index].find(search) != std::string::npos) {
        if(index > 0) // There's no line before the first line, so we can't print it
            cout << index-2 << "     " << lines[index-2] << endl;
            cout << index-1 << "     " << lines[index-1] << endl;
            cout << index << "     " << lines[index] << endl;
        
        if(index < sz-1) // There's no line after the last line, so we can't print it
            cout << index+1 << "     " << lines[index+1] << endl;
            cout << index+2 << "     " << lines[index+2] << endl;
    }
}

This is the code..

But it fails, it says >> str undeclared, first use this function <<

I'm i doing something bad ??

>> But it fails, it says >> str undeclared, first use this function <<
I'm i doing something bad ??

You get this error because str is not declared anywhere in your code. Add a std::string str; declaration statement before you use getline() .

>> But it fails, it says >> str undeclared, first use this function <<
I'm i doing something bad ??

You get this error because str is not declared anywhere in your code. Add a std::string str; declaration statement before you use getline() .

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