A find program

So I have to do this :

Write a program find, which searches the files and prints all lines of these files containing a given word. The program is invoked with the command-line arguments. For example

find George report.txt all.txt me.txt
looks for the word George in the files report.txt, all.txt и me.txt.

Can somebody do this, please ? It's urgent !
There's a rule against doing other people's homework on here. You need to at least attempt to do this on your own.
lol. do you know programming? Then you should be able to do this. try it and, maybe someone will help ;)
At least where can I look for a tutorial or any explaination ?
Do you already know how to even make .txt files? If not I'd look into http://www.cplusplus.com/doc/tutorial/files/
do you know programming?
Did you study such standard algorithm as std::find or std::any_of?
All what you need is to use function getline to read records sequentially and then apply either std::find or std::any_of for the read record considered as std::istringstream.
Last edited on
or, if you need to implement the algorithm yourself, compare each char, although I have no especial expertise in that field ;) But that would be my, at least first, approach to doing what you want to do.
In my opinion the assignment is too complicated for beginners. I can list some C++ constructions that I would use to perform the assignment.

1. std::ifstream
2. std::vector
3. std::reference_wrapper
4. std::istringstream
5. std::istream_iterator
6. lambda expressions
7. std::any_of
8, the range-based for statement.
I would tend to agree. if you don't have ANY knowledge, how in the *** can anyone do 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
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
    #include <iostream>
    #include <fstream>
     
    using namespace std;
       
    int main()
     
    {
    ifstream fin;
    char filename[50];
    string searchword;
    string s;
    int count;
     
    cout << "Enter filename : ";
    cin >> filename;
     
    fin.open(filename);
     
    string word();
    searchword = word();
     
       if (!fin.good())
    {
    cout << "File not found" << endl;
    system("pause");
    return 1;
    }
     
    while(!fin.eof())
    {
      
    getline(fin, s);
     
       int i, position;
       
    for (i=0; i<s.length();i++)
    s[i]=tolower(s[i]);
    for (i=0; i<searchword.length();i++)
    searchword[i]=tolower(searchword[i]);
  
    position = s.find(searchword);
     
     
    if (position != string :: npos)
     
    { cout << s << endl; } }
     
      fin.close();
       
    cout << "End Of Search." << endl;
     
    system("pause");
    return 0;
     
    }
     
    string word()
    {
    string search;
    cout<< "Type in your Search Word: " << endl;
    cin >> search;
    return search;
    }


Is this what I need ?
In your assignment there is writtten

The program is invoked with the command-line arguments. For example

find George report.txt all.txt me.txt


Also what is the word? For example used by you function will report that it has found the word "back" in line "seacrh backword". However if to consider the word as separated by spaces then there is no word "back" in the line.
I have no idea how to put the argument ..
First of all you should check that there were set at least two command line parameters that is argc shall be equal to or greater than 3.
Then in loop you can open files taking their names from the parameters.

For example

1
2
3
4
5
6
for ( int i = 2; i < argc; i++ )
{
   std::ifstream fin( argv[i] );
   if ( !fin ) /* issue some error ir warning message */;

   // and so on  
Last edited on
Topic archived. No new replies allowed.