0

I'm trying to read a text file to find how many times a phrase/sentence(/substring?) occurs. I've done a real bodge job on it currently (see code below) but as you'll see, it relies on some rather clunky if statements.

I don't have access to the files I''ll be using it on at home, so I've used a file called big.txt and search for phrases like "and the" for the time being.

Ideally, I'd like to be able to search for "this error code 1" and it return the number of times it occurs. Any ideas on how I might get my code to work that way would be incredibly useful!

int fileSearch(string errorNameOne, string errorNameTwo, string textFile) { string output; //variable that will store word from text file ifstream inFile; inFile.open(textFile); //open the selected text file if (!inFile.is_open()) { cerr << "The file cannot be opened"; exit(1); } if (inFile.is_open()) { //Check to make sure the file has opened correctly while (!inFile.eof()) { //While the file is NOT at the end of the file inFile >> output; //Send the data from the file to "output" as a string if (output == errorNameOne) { //Check to look for first word of error code marker = 1; //If this word is present, set a marker to 1 } else if (marker == 1) { //If the marker is set to 1, if (output == errorNameTwo) { //and if the word matches the second error code... count++; //increse count } marker = 0; //either way, set marker to 0 again } } } inFile.close(); //Close the opened file return count; //Function returns count of error } 
7
  • I'm not able to understand your problem. Can you please add some example with some dummy data like: what is your input and what is expected output. Commented Sep 14, 2014 at 13:58
  • You say you're looking for this phrase: "this error code 1" yet your example is looking for two phrases one after the other. Are you looking for one or two phrases? Also how variable is the phrase in the text file? Is it always precisely fixed or can the number of spaces change (for example). Is the number fixed at 1 or can it be other numbers like: "this error code 7"? Commented Sep 14, 2014 at 14:01
  • @Galik in the actual log files, the phrase would read (for example) "Header Tangs Present" then a load of whitespace and then a 1. It can only have a 1 or a 0 and all of the error codes are listed line by line in what looks like a table (but it's just a formatted text file). the reason I'm looking for two phrases is that when the string is sent to output from inFile it only ever does one word at a time, so it won't recognise the second word, hence the marker and the check of the next word. Commented Sep 14, 2014 at 14:15
  • So is it true that there is only ever one phrase per line? Or can the phrase appear several times on each line? Commented Sep 14, 2014 at 14:29
  • @Galik the phrase in question would only ever appear once on a line. I can't just match the entire line though as it also begins with a time-stamp, which is different for every instance. The line might read: <product information>...<timestamp>...header tangs present 1 <additional information> Commented Sep 14, 2014 at 14:42

1 Answer 1

2

Given that your phrase can only occur once per line and the number follows the phrase after a number of spaces you can read the file line by line and use std::string::find() to see of your phrase is somewhere in the line. That will return the position of the phrase. You can then work on checking the rest of the line immediately after the phrase to test the number for 1 or 0.

This code may not be exactly what you want (still not certain of the exact specs) but hopefully it should contain enough examples of what you can do to achieve your goal.

// pass the open file stream in to this function along with the // phrase you are looking for and the number to check int count(std::istream& is, const std::string& phrase, const int value) { int count = 0; std::string line; while(std::getline(is, line)) // read the stream line by line { // check if the phrase appears somewhere in the line (pos) std::string::size_type pos = line.find(phrase); if(pos != std::string::npos) // phrase found pos = position of phrase beginning { // turn the part of the line after the phrase into an input-stream std::istringstream iss(line.substr(pos + phrase.size())); // attempt to read a number and check if the number is what we want int v; if(iss >> v && v == value) ++count; } } return count; } int main() { const std::string file = "tmp.txt"; std::ifstream ifs(file); if(!ifs.is_open()) { std::cerr << "ERROR: Unable to open file: " << file << '\n'; return -1; } std::cout << "count: " << count(ifs, "Header Tangs Present", 1) << '\n'; } 

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.