[英]Reading from an input file one word at a time in c++
我正在嘗試從輸入文件中將一組單詞讀入3個不同的字符串數組。 文件中的單詞用“#”分隔。 由於某種原因,我的代碼運行了兩次,第一個數組為空,單詞為。 請幫助,我的循環肯定是錯誤的,我必須忽略一些東西。 請讓我知道我在做什么錯。 謝謝
Sample input file (input.txt)
complicated
insinuated
complex
juggernaut
#
blah
...
...
#
...
#include <iostream>
#include <fstream>
#include <string>
#include "conio.h"
using namespace std;
int main() {
ifstream inFile("dictionary.txt");
// Check for error
if (inFile.fail()){
cout << "Error Opening File." << endl;
exit(1);
}
string hard[27], medium[29], easy[33];
string getHardWord, getMedWord, getEasyWord;
int hardCount = 0, medCount = 0, easyCount = 0, delimitCount; // n is number of #
// delimiters and count is array position
// Read the dictionary file until the end of file
while (inFile){
inFile >> getHardWord;
while ((getHardWord != "#") && (delimitCount = 0)){
hard[hardCount] = getHardWord;
hardCount++;
inFile >> getHardWord;
}
delimitCount++;
cout << delimitCount << endl;
for (int iii = 0; iii < 27; iii++){
cout << hard[iii] << endl;
}
cout << endl;
inFile >> getMedWord;
while ((getMedWord != "#") && (delimitCount = 1)){
medium[medCount] = getMedWord;
medCount++;
inFile >> getMedWord;
}
delimitCount++;
cout << delimitCount << endl;
for (int jjj = 0; jjj < 27; jjj++){
cout << medium[jjj] << endl;
}
cout << endl;
inFile >> getEasyWord;
while ((getEasyWord != "#") && (delimitCount = 2)){
easy[easyCount] = getEasyWord;
easyCount++;
inFile >> getEasyWord;
}
delimitCount++;
cout << delimitCount << endl;
for (int kkk = 0; kkk < 27; kkk++){
cout << easy[kkk] << endl;
}
inFile.close();
}
_getch();
return 0;
}
此代碼和示例文本文件中存在一些小錯誤:
1-示例文件的末尾應帶有#,否則最后一個循環將永遠運行。
2-
while ((getHardWord != "#") && (delimitCount = 0))
由於delimitCount將為零,因此將始終為false。 您應該在其聲明中將delimitCount初始化為零,並在此循環中刪除賦值,以使其變為:
while (getHardWord != "#")
3-如果要在最后關閉文件,則while條件應該是檢查文件是否打開,所以不要:
while (inFile)
采用
while (inFile.is_open())
我通過這些更改對您的代碼進行了測試,效果很好:
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include "conio.h"
using namespace std;
int main() {
ifstream inFile("dictionary.txt");
// Check for error
if (inFile.fail()){
cout << "Error Opening File." << endl;
exit(1);
}
string hard[27], medium[29], easy[33];
string getHardWord, getMedWord, getEasyWord;
int hardCount = 0, medCount = 0, easyCount = 0, delimitCount=0; // n is number of #
// delimiters and count is array position
// Read the dictionary file until the end of file
while (inFile.is_open()){
inFile >> getHardWord;
while ((getHardWord != "#")){
hard[hardCount] = getHardWord;
hardCount++;
inFile >> getHardWord;
}
cout<<hard<<endl;
cout<<endl;
delimitCount++;
cout << delimitCount << endl;
for (int iii = 0; iii < 27; iii++){
cout << hard[iii] << endl;
}
cout << endl;
inFile >> getMedWord;
while ((getMedWord != "#") && (delimitCount = 1)){
medium[medCount] = getMedWord;
medCount++;
inFile >> getMedWord;
}
delimitCount++;
cout << delimitCount << endl;
for (int jjj = 0; jjj < 27; jjj++){
cout << medium[jjj] << endl;
}
cout << endl;
inFile >> getEasyWord;
while ((getEasyWord != "#") && (delimitCount = 2)){
easy[easyCount] = getEasyWord;
easyCount++;
inFile >> getEasyWord;
}
delimitCount++;
cout << delimitCount << endl;
for (int kkk = 0; kkk < 27; kkk++){
cout << easy[kkk] << endl;
}
inFile.close();
}
_getch();
return 0;
}
您不使用getline是有原因的嗎? 如果我正確理解您的代碼,則可以通過僅使用#作為該函數中的定界符來簡化該代碼。
for (int i = 0; i < 27; i++) //It looks like there are 27 words per type, if that's wrong change this
{
getline(inFile, getHardWord, '#');
hard[i] = getHardWord;
}
//And so on for the other difficulties.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.