因此,我想出了以下代碼以打開文本文件並將其保存並使用數組來打印出所有文本。我的問題是,如何訪問文件中的特定單詞或文本。如果我沒有弄錯,應該有一個for循環,但我不太清楚如何去做。C++將文本文件保存在數組中
int main() {
ifstream dictionaryFile;
dictionaryFile.open("dictionary.txt");
char output[100];
//char wordsFromDictionary[40437][22];
int i=0;
if(dictionaryFile.is_open()){
while(!dictionaryFile.eof()){
dictionaryFile >> output;
cout<<output<<endl;
}
}
return 0;
}
爲什麼要涉及'for'循環? –
現在代碼只顯示文本文件中的所有內容。所以我不應該使用for循環來挑選第n個元素? –
'for'循環不比'while循環更有效率。你可以用'if'語句來使用'strcmp'。 –