-1

I'm new to C++, and I need to find the one word inputted (searchWord) within the text file "input.txt".

I have it now where it will output every word in the file, but I need it to just output the inputted string and find it.

#include<iomanip>
#include<fstream>
#include<string>
using namespace std;

int main(){
    // Variable Declaration
    string fileName;
    string searchWord;
    int exitValue = 0;

    // Name the file to open
    ifstream inputFile;

    // Prompt the user to enter the file name
    cout << "Enter the name of the file: ";
    cin >> fileName;

    // Logic to determine if the file name equals "input.txt"
    if (fileName == "input.txt"){ // if user input = input.txt
        inputFile.open("input.txt"); // opening input.txt
    }
    else{
        cout << "\nCould not open '" << fileName << "'." << endl;
        exit(0);
    }

    // Prompt the user for a word to search for in the file
    cout << "Enter a value to search for: ";
    cin >> searchWord;

    // Logic to determine if the inputted string is in the file or not
    while (inputFile >> searchWord){
        cout << "\n'" << searchWord << "' was found in '" 
            << fileName << "'." << endl;
    }

    inputFile.close();
    return 0;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • An exercise for you: write a function that delimits a given string by a given delimiter (like space or newline). Try to solve your task by using that function. Not the quickest approach, and there are libraries who will do that better, but will teach you some basics with which you can solve related problems in the future. – Aziuth Sep 14 '20 at 16:11
  • Does this answer your question? [read word by word from file in C++](https://stackoverflow.com/questions/20372661/read-word-by-word-from-file-in-c) – midor Sep 14 '20 at 16:56

2 Answers2

1

Do it just a bit different:

// Logic to determine if the inputted string is in the file or not
string inputWord;
while (inputFile >> inputWord){ // Do not overwrite the given searchWord
    if(inputWord == searchWord ) { // Check if the input read equals searchWord     
        cout << "\n'" << searchWord << "' was found in '" 
             << fileName << "'." << endl;
        break; // End the loop
    }
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

This worked for me:

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

// Returns the index of the value you are looking for.
// If the value isn't found, it returns -1.
int searchFile(string filename, string value) {
    FILE *fp;
    int c, i = 0;
    string contents = "";

    fp = fopen(filename.c_str(), "r");
    while (true) {
        c = fgetc(fp);
        contents += c;
        if (feof(fp)) { 
            break;
        }

        i++;
        if (i >= value.length() && contents.substr(i - value.length(), i) == value) {
            return i;
        }
    }
    fclose(fp);
    return -1;  // value not found in file
}

int main() {
    string fileName;
    string value;
    cout << "Enter file name: ";
    cin >> fileName;
    cout << endl << "Enter search value: ";
    cin >> value;

    int index = searchFile(fileName.c_str(), value.c_str());
    if (index == -1) cout << "Not found";
    else cout << "Found at index " << index << endl;
    cin.get();
    cin.get();
    return 0;
}
Thomas
  • 73
  • 3