【发布时间】:2016-06-04 18:11:10
【问题描述】:
我正在尝试将来自用户输入的单词与来自文本文件的字符串进行匹配。
当此代码运行时,它会在文件打开后崩溃。 (以****标记) 如何更改它以正确匹配用户输入的字符串与文本文件中的字符串。
任何帮助将不胜感激,谢谢。
const int Size = 81; // 80 characters for the line + 1 for the '\0'
const int MaxNumberOfWords = 10;
int main() {
char input[81], temp[81], fin[81];
printf("Input a string\n");
fgets(input, 81, stdin);
int len = strlen(input);
char *div;
div = strtok(input, " ");
while (div != NULL) {
printf("%s\n",div);
div = strtok(NULL, " ");
ifstream inStream; // declare an input stream for my use
char theWords[ MaxNumberOfWords][ Size]; // Array to store words from input line
int wordRow = 0; // Row for the current word
char wordToLookup[ Size]; // word to lookup
bool wordWasFound = false; // flag to track whether or not word is found
char c; // stores return character after input
inStream.open( "C:\\Users\\dqiao4\\Desktop\\Dev-Cpp\\dictionaryMax6.txt");
assert( ! inStream.fail() ); // make sure file open was OK
//*****this is where the code crashes
while ( inStream >> theWords[ wordRow]) {
wordRow++;
}
for (int i=0; i<wordRow; i++) {
// See if this word matches
if ( strcmp( div, theWords[ i]) == 0 ){
wordWasFound = true;
break; // quit looking
}
}
}
}
【问题讨论】:
-
请展示您的研究成果。请先阅读How to Ask页面。
-
但是您的具体问题是什么?如果您正在寻找某人为您编写代码,那么您来错地方了。请尝试自己编写代码。如果经过真正的努力后您仍然遇到困难,请展示您的代码并提出具体问题。
-
@thedude 强烈建议不要使用代码图片。我们应该如何将其复制并粘贴到我们的 IDE 中?
-
@thedude C 还是 C++?根据您选择的语言,您还会得到非常不同的答案。
-
不要将代码作为图片发布。你还没有问过一个问题。您需要什么帮助?
标签: c++