0

I'm trying to write some code that will determine if a string (and/or char array) is present in a text file, but I'm stuck here. Any advice? I'm also pretty new to c++ so it would be nice if you guys could keep it simple for me :) thanks

Okay so the following code is edited from what I previously posted, and I'm editing to provide further context. Firstly, I know this code doesn't work. Obviously. Second, the object of my task is to scan every line in the first text file (represented by char* puzzle) and determine if each line is a word (by checking if it is present in dict, which is also a text file). The issue is I don't know how to do this.

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;


void checker(char* puzzle, char* dict){
   int x;
   fstream newfile;
   newfile.open(puzzle, ios::in); 
   string tp;

   if (newfile.is_open()){ 
      while(getline(newfile, tp)){ 

        if ((x = tp.find(tp, 0)) != string::npos) {
           cout << "found '" << dict << " \n\n" << tp << endl;
        } else {
            cout << "Not found \n\n";
        }

      }
      newfile.close(); 
   }
}


int main(int argc, char* argv[]){
   checker(argv[1], argv[2]);
}
6
  • char cstr[tp.size() + 1]; is not valid c++. Also, you should use std::string instead of char*, it's much easier. Commented Sep 10, 2020 at 21:50
  • Even if that were valid C++, you haven't put anything in tp yet, so tp.size() is 0. You would have to put that line after calling getline(). Commented Sep 10, 2020 at 21:51
  • What string are you searching for? tp.find(cstr, 0) makes no sense, since cstr is a copy of tp. Commented Sep 10, 2020 at 21:52
  • 1
    Shouldn't you be searching for dict, not cstr? Commented Sep 10, 2020 at 21:52
  • Yeah I know the code is awful and doesn't work properly, hence why I'm asking. Basically, I want to determine if each line in puzzle is a real word by checking if it is present in dict. Commented Sep 10, 2020 at 21:54

2 Answers 2

1

There's no need to make a copy of tp().

You should be searching for dict with tp.find(), not cstr.

You can't know that the string is not found until you get to the end of the loop. You shouldn't print Not found on every line that doesn't match. Use a variable to record whether the string was found during the loop.

The default starting position in find() is 0, there's no need to specify it explicitly.

void checker(char* puzzle, char* dict){
    fstream newfile;
    newfile.open(puzzle, ios::in); 

    if (newfile.is_open()){ 
        string tp;
        bool found = false;
        while(getline(newfile, tp)){ 
            if (tp.find(dict) != string::npos) {
                cout << "found '" << dict << "'\n\n" << tp << endl;
                found = true;
            }
        }
        newfile.close();
        if (!found) {
            cout << "Not found\n\n";
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Quite a few things can be improved:

  • you can check for success of opening a file on the same line that constructs the ifstream object, and don't need to specify in explicitly - just use an ifstream instead of fstream
  • function arguments you don't intend to change can be passed as const
  • you can search for the text without copying anything to a local char array
  • if you're going to print every match, you need to keep track of whether you've seen a match yet; so I introduce a found boolean; a int num_matches = 0; variable that gets increments (i.e. ++num_matches;) each time there's a match is another option that captures a bit more information
  • there's normally no need to explicitly close a stream - the destructor will do that for you as the ifstream object goes out of scope (i.e. after the if-statement body in which it's created)
  • it's good to create things just as you're ready to use them - hence while (std::string line; getline(file, line))

Code:

#include <iostream>
#include <fstream>
#include <string>

void checker(const char* puzzle_filename, const char* search_term){
   if (auto file = std::ifstream{puzzle_filename}) {
      bool found = false;
      while (std::string line; getline(file, line)) { 
         if ((line.find(search_term)) != string::npos) {
            std::cout << "found '" << search_term << "' \n\n" << tp << std::endl;
            found = true;
         }
      }
      if (!found)
         std::cout << "Not found\n\n";
   }
   else
       std::cerr << "unable to open " << puzzle_filename << '\n';
}


int main(int argc, char* argv[]){
   checker(argv[1], argv[2]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.