作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:更改为 bool 标志,但打印的内容仍然多于应有的内容:
string line; // a string to hold the current line
while(getline(myFile,line)) {
bool old_count = false; // to help determine whether the line has been output yet
for (unsigned int i = 0; i < line.length(); i++) {
string test = line.substr( i, targ_length );
if ( strcmp(word.c_str(),test.c_str()) == 0 ) {
count++;
if ( !old_count ); {
cout << line_num << " : " << line << endl;
} // end if
old_count=true;
} // end if
} //end for
line_num++;
} // end while
/结束编辑
我的作业是编写一个程序来搜索文本文件中的单词。我已经让它完美地工作,除了它应该打印找到该单词的每一行,如果该单词在该行中出现多次,我的程序将多次打印同一行。我需要它只打印一行。我试过将 if (count != old_count) 移动到不同的地方,但运气不好,我很困惑。我的代码如下。谢谢你的帮助!
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
/* minimum required number of parameters */
#define MIN_REQUIRED 3
/* display usage */
int help() {
printf("Proper usage: findWord <word> <file>\n");
printf("where\n");
printf(" <word> is a sequence of non-whitespace characters\n");
printf(" <file> is the file in which to search for the word\n");
printf("example: findWord the test.txt\n");
return 1;
}
/*
* Program that searches for occurrences of given word within a given file
* @return 0 (default for a main method)
*/
int main(int argc, char *argv[]) {
if (argc < MIN_REQUIRED) {
return help();
} // end if
string word = argv[1]; // the word to be searched for
string file_name = argv[2]; // the name of the file to be read
ifstream myFile(file_name.c_str()); // read the file
if (! myFile) {
cerr << "File '" << file_name << "' could not be opened" << endl;
return -1;
} // end if
cout << "Searching for '" << word << "' in file '" << file_name << "'\n";
int targ_length = word.length(); // the legnth of the string we're searching for
int count = 0; // running count of instances of word found
int line_num = 1; // number of current line
string line; // a string to hold the current line
while(getline(myFile,line)) {
int old_count = count; // to help determine whether the line has been output yet
for (unsigned int i = 0; i < line.length(); i++) {
string test = line.substr( i, targ_length );
if ( strcmp(word.c_str(),test.c_str()) == 0 ) {
count++;
} // end if
if ( old_count != count ); {
cout << line_num << " : " << line << endl;
} // end if
} //end for
line_num++;
} // end while
cout << "# occurrences of '" << word <<" ' = " << count << endl;
return 0;
}
最佳答案
尝试将 int
标志更改为 bool
标志,如下所示:
while(getline(myFile,line)) {
bool old_count = false; // to help determine whether the line has been output yet
bool second_flag = false;
for (unsigned int i = 0; i < line.length(); i++) {
string test = line.substr( i, targ_length );
if ( strcmp(word.c_str(),test.c_str()) == 0 ) {
old_count = true;
} // end if
if ( old_count && !second_flag ){
cout << line_num << " : " << line << endl;
second_flag = true;
} // end if
} //end for
line_num++;
} // end while
关于c++ - 在巢中放置 cout 的适当位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5914499/
我是一名优秀的程序员,十分优秀!