从文件中读取一个单词
Reading in one word from a file
我是 C++ 新手,我需要在文本文件“input.txt”中找到输入的单词 (searchWord)。
我现在有了它,它将输出文件中的每个单词,但我需要它来输出输入的字符串并找到它。
#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;
}
稍微有点不同:
// 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
}
}
这对我有用:
#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;
}
我是 C++ 新手,我需要在文本文件“input.txt”中找到输入的单词 (searchWord)。
我现在有了它,它将输出文件中的每个单词,但我需要它来输出输入的字符串并找到它。
#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;
}
稍微有点不同:
// 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
}
}
这对我有用:
#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;
}