[英]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.