CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2013
    Posts
    5

    work with text file

    Hello, I have a text file where in each line is 3 words and 2 int type numbers. I'm doing search by word and I want to print out to the screen the whole line where that word was found (the words is repeated in some lines). My search looks like this:
    Code:
     #include<iostream> 
    #include<string> 
    #include<fstream>
     #include<stdlib.h>
     #include <vector> 
    using namespace std; 
    int main(){
          vector <string> words; 
          string str, paieska;
                  ifstream fin("duom.txt"); 
                    while (fin >> str) 
                    { words.push_back(str); }
          cout<<"Iveskite paieskos zodi"<<endl; 
          cin>>paieska;
                    for (int i = 0; i < words.size(); ++i){
                     if (paieska==words.at(i)){
                    cout<<"Rasta: "<<words.at(i)<<endl<<i<<endl;}}
                fin.close(); 
    return 0;
     system("pause"); }
    How to print line(s) where I found that word?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: work with text file

    You should read in the whole line, then parse it (knowing that there are 3 words and 2 int type numbers), then do search by word, and it you'll find it - print the whole line.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2013
    Posts
    5

    Re: work with text file

    How to save whole line in vector, is there a function for that?
    Last edited by martynasj; April 7th, 2013 at 03:51 AM.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: work with text file

    Quote Originally Posted by martynasj View Post
    How to save whole line in vector, is there a function for that?
    You don't need to "save whole line in vector". Just save one line a time in some string object. Then parse this string object to search for the words you are interesting in!
    If such a word has been found - print out the whole string containing the line. If not - read in the next line to the same string object.
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2013
    Posts
    5

    Re: work with text file

    I tried do like this:

    Code:
    ifstream fin("duom.txt"); 
      while (!fin.eof()){
      
    	
    		getline(fin, lin); 
    		words.push_back(lin);	
    		cout<<"What word you are looking for?"<<endl;
        cin>>paieska;
    	
      	for(int j=0; j< words.size(); ++j){
    	if (paieska==words.at(j)){
        cout<<"found: "<<lin<<endl;}}}
    but It asks several times to put words and dont find any maches.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: work with text file

    I wrote you twice that you have to parse the string you have read in to find all its single words!
    So why do you ignore my suggestion?
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 2013
    Posts
    5

    Re: work with text file

    Sorry I don't realy understand what you trying to say me.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: work with text file

    Then first learn what parsing is and how to parse...
    Victor Nijegorodov

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: work with text file

    Parsing is to break apart one item into its constituent parts. So if you read a complete line from the file into a string called line, then parsing is to obtain from the string line the individual 3 words as strings (you don't need to obtain the 2 ints as you don't use them).

    Alternativly, you could just read from the file the 3 words and the 2 ints as seperate variables (3 strings and 2 ints) (fin >> s1;fin >> s2;fin >> s3; fin >> i1; fin >> i2 and then compare each of the strings read (s1, s2, s3) against the search word. If found then just print the 3 strings and the 2 ints.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Apr 2013
    Posts
    5

    Re: work with text file

    Thank you I will try that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured