0

here's a part from my code

string word;
cin >> word;
string keyword;
while (file >> keyword && keyword != word){}

This searches for a word in a file and if it finds that word (keyword) then it starts a string from there later. It's working perfectly at the moment. My problem is that when the line is

"Julia","2 Om.","KA","1 Om. 4:2"

if I enter word Julia I can not find it and use it for my purposes (just FYI I'm counting it). It works if I search for "Julia","2 since this is where space comes in.

I'd like to know how can I change line

while (file >> keyword && keyword != word){}

so I can see when the text/string CONTAINS that string since at the moment it only finds and accepts it if I enter the WHOLE string perfectly.

EDIT: Also what I have found this far is only strstr, strtok, strcmp. But these fit more with printf than with cout.

Jack Longen
  • 51
  • 3
  • 11
  • So if your enter `Julia`, you want it to stop with `keyword` as `"Julia","2`? Or do you want it to stop with `keyword` as `"Julia"`? – Joseph Mansfield Apr 07 '13 at 23:14
  • No, if I enter Julia I want it to find Julia - at the moment it does not find it, it is able to find "Julia","2 since that is the whole part but I do not need that. – Jack Longen Apr 07 '13 at 23:16

3 Answers3

3

You can use methods from std::string like find.

#include <string>
#include <iostream>
// ...
std::string keyword;
std::string word;

getline(file, keyword);
do
{
    std::cin >> word;
}
while (keyword.find(word) == std::string::npos);
Julien Fouilhé
  • 2,583
  • 3
  • 30
  • 56
  • Although this is what I thought they were describing at first, I don't think it's what they want. They don't want `"Julia","2` to match as `Julia`. – Joseph Mansfield Apr 07 '13 at 23:18
  • @sftrabbit He wants to find Julia even if it's surrounded by other characters, and this finds Julia even if it's surrounded by other characters. – Patashu Apr 07 '13 at 23:19
  • Yes, that's what I meant Patashu. Sorry for confusion and thank you for your answers :) I'll try to get it working now. – Jack Longen Apr 07 '13 at 23:21
  • @JackLongen I still don't think this is what you want. You will still be extracting `"Julia","2` and not `"Julia"`. The keyword `2 Om.` will not be found using this method at all. – Joseph Mansfield Apr 07 '13 at 23:24
1

The problem is that you're extracting strings, which by default will extract up until the next space. So at the first iteration, keyword is "Julia","2. If you want to extract everything separated by commas, I suggest using std::getline with , as the delimeter:

while (std::getline(file, keyword, ','))

This will look through all of the quoted strings. Now you can use std::string::find to determine if the input word is found within that quoted string:

while (std::getline(file, keyword, ',') &&
       keyword.find(word) == std::string::npos)

Now this will loop through each quoted string until it gets to the one that contains word.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • Hmmm, I'm trying to get it working at the moment. Also 2. Om is not supposed to be keyword, if I understood you from your last comment. My input is Julia and my output is actually int cnt = 1+ count( istream_iterator(file), istream_iterator(), word); cout << word << " appears in text " << cnt << " times"; as you can see I'm writing Julia and then want the wordcount of just Julia but it doesn't find Julia if it's surrounded by other characters. – Jack Longen Apr 07 '13 at 23:37
  • So I actually don't want the loop to end there since I need to count all Julia's in a file and there are loads of these names in my file. – Jack Longen Apr 07 '13 at 23:47
  • Thank you for your help! Now I am able to find Julia in that file but it only counts it once and stops, in the line where the name is first found. Since there are like 10 Julia lines in that file (20 000 lines, all different names), I still need for it to loop, any idea how could I do that? – Jack Longen Apr 07 '13 at 23:58
0

Use this method of istream to get a whole line instead of just a single "word":

http://www.cplusplus.com/reference/istream/istream/getline/

Then use strstr, to find the location of a string (like Julia) in a string (the line of the file):

http://www.cplusplus.com/reference/cstring/strstr/

Patashu
  • 21,443
  • 3
  • 45
  • 53