-
April 7th, 2013, 02:25 AM
#1
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?
-
April 7th, 2013, 02:38 AM
#2
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
-
April 7th, 2013, 03:38 AM
#3
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.
-
April 7th, 2013, 04:23 AM
#4
Re: work with text file
Originally Posted by martynasj
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
-
April 7th, 2013, 06:29 AM
#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.
-
April 7th, 2013, 06:39 AM
#6
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
-
April 7th, 2013, 06:46 AM
#7
Re: work with text file
Sorry I don't realy understand what you trying to say me.
-
April 7th, 2013, 06:50 AM
#8
Re: work with text file
Then first learn what parsing is and how to parse...
Victor Nijegorodov
-
April 7th, 2013, 07:01 AM
#9
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)
-
April 7th, 2013, 07:18 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|