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]);
}
char cstr[tp.size() + 1];is not valid c++. Also, you should usestd::stringinstead ofchar*, it's much easier.tpyet, sotp.size()is0. You would have to put that line after callinggetline().tp.find(cstr, 0)makes no sense, sincecstris a copy oftp.dict, notcstr?