さて、私の単純なプロジェクトは、特定の文字列が .txt ファイルで見つかったすべてのケースを検索することになっています。大文字と小文字が区別され、その単語が別の単語の中にあるかどうかが重要です。
(例: 単語が「the」の場合:
有効な検索結果は次のとおりです。
りんご = 1;
演劇人 = 2;
無効な検出には次のものが含まれます。
4 番目の象 (th と e の間のスペース)
りんご (大文字)
ファイルの行に IS という単語が見つかった場合は、その行を 1 回出力することになっています。見つからない場合は、印刷することはまったくありません。
したがって、たとえば、私のプログラムを 1 回実行すると、次のように出力されます。
Searching for 'the' in file 'test.txt'
2 : that they do not use permeates the [C++] language. Another example
3 : will further illustrate this influence. Imagine that an integer
5 : What bit value should be moved into the topmost position? If we
6 : look at the machine level, architectural designers are divided on
8 : the most significant bit position, while on other machines the sign
9 : bit (which, in the case of a negative number, will be 1) is extended.
10 : Either case can be simulated by the other, using software, by means
# occurrences of 'the' = 13
残念ながら、私は得ています
Searching for 'the' in the file 'test.txt'
2: that they do not use permeates the [C++] language. Another example
3: will further illustrate this influence. Imagine that an integer
5: What bit value should be moved into the topmost position? If we
6: look at the machine level, architectural designers are divided on
8: the most significant bit position, while on other machines the sign
9: bit (which, in the case of a negative number, will be 1) is extended.
10: Either case can be simulated by the other, using software, by means
11: of a combination of tests and masks.
12:
# occurrences of 'the' = 15
11行目と12行目で「the」が見つかったと考える理由がわかりません。
これが私のコードです:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char* argv[]){
//a char pointer is a c-string
//the array is just an array of char pointers
//argv[0] = pointer to the word to search for
//argv[1] = pointer to fileNames
//includes program name @ 0, so three args
if (argc == 3){
int wordCounter = 0;
ifstream myFile(argv[2]);
if (!myFile){
cout << "File '" << argv[2] << "' could not be opened" << endl;
return 1;
}
else {
//counts the number of lines in file
int counter = 0;
//holds the new line in the file
char line[100];
//copies string into buffer that is length of word
const char * word = argv[1];
//holds whether found word
bool found = false;
cout << "Searching for '" << word << "' in the file '" << argv[2] << "'" << endl;
//number of chars in a line
int numChar = 0;
//saves every line
while (!(myFile.getline(line, 100)).eof()) {
//starts every new new at not having found the word
found = false;
//read in new line, so increases line counter
counter ++;
numChar = 0;
//find length of line
for (int i = 0; line[i] != '\n' && i < 101; i++){
numChar++;
}
//finds how many times the key word appears in one line
//checks up to a few before the end of the line for the word
if (numChar >= strlen(argv[1])){
for (int i = 0; i < numChar - strlen(argv[1]); i++){
//if the current line letter equals the first letter of the key word
if (line[i] == word[0]){
//continue looking forward to see if the rest of it match
for (int j = 0; j < strlen(argv[1]); j++){
//if word doesn't match break
if (word[j] != line [i+j]){
break;
}
//if matches all the way to end, add counter
if(j == strlen(argv[1]) - 1){
wordCounter++;
found = true;
}
}//end 2ndfor
}
}//end 1stfor
//if the key word has been found, print the line
if (found){
cout << counter << ": " << line << endl;
}
}
}//endwhile
cout << "# occurrences of '" << word << "' = " << wordCounter << endl;
myFile.close();
}//end else
}//end if
return 0;
}//end main