gpt4 book ai didi

c++ - 在 C++ 中一次从输入文件中读取一个单词

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:49 24 4
gpt4 key购买 nike

我正在尝试将输入文件中的一组单词读入 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))

将始终评估为 false,因为 delimitCount 将为零。您应该在其声明中将 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;
}

关于c++ - 在 C++ 中一次从输入文件中读取一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19557622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com