C++ 在c+中一次读取一个输入文件中的一个字+;

C++ 在c+中一次读取一个输入文件中的一个字+;,c++,C++,我试图从输入文件中将一组单词读入3个不同的字符串数组。文件中的单词用“#”分隔。出于某种原因,我的代码运行了两次,第一个数组为空,单词为空。请帮忙,我的循环肯定是错的,我一定是忽略了什么。请让我知道我做错了什么。谢谢 Sample input file (input.txt) complicated insinuated complex juggernaut # blah ... ... # ... #include <iostream> #include <fst

我试图从输入文件中将一组单词读入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;
}
示例输入文件(input.txt)
复杂的
含沙射影
复杂的
无法控制的强大机构
#
废话
...
...
#
...
#包括
#包括
#包括
#包括“conio.h”
使用名称空间std;
int main(){
ifstream-infle(“dictionary.txt”);
//检查错误
if(infle.fail()){
库特格特哈德;
而((GetHardward!=“#”)&&(定界计数=0)){
硬[硬计数]=GetHardward;
硬计数++;
填充>>格特哈德;
}
计数++;

cout此代码和示例文本文件中有几个小错误:

1-示例文件的结尾应该有一个#,否则最后一个循环将永远运行

二,-

将始终计算为false,因为diffickCount将为零。您应该在其声明中将diffickCount初始化为零,并在该循环中删除赋值,使其成为:

while (getHardWord != "#")
3-如果要在最后关闭文件,则while条件应是检查文件是否打开,而不是:

while (inFile)
使用

我用这些更改测试了您的代码,效果很好:

// 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;
}
//Test.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
#包括
#包括“conio.h”
使用名称空间std;
int main(){
ifstream-infle(“dictionary.txt”);
//检查错误
if(infle.fail()){
库特格特哈德;
而((GetHardward!=“#”)){
硬[硬计数]=GetHardward;
硬计数++;
填充>>格特哈德;
}

cout此代码和示例文本文件中有几个小错误:

1-示例文件的结尾应该有一个#,否则最后一个循环将永远运行

二,-

将始终计算为false,因为diffickCount将为零。您应该在其声明中将diffickCount初始化为零,并在该循环中删除赋值,使其成为:

while (getHardWord != "#")
3-如果要在最后关闭文件,则while条件应是检查文件是否打开,而不是:

while (inFile)
使用

我用这些更改测试了您的代码,效果很好:

// 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;
}
//Test.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
#包括
#包括“conio.h”
使用名称空间std;
int main(){
ifstream-infle(“dictionary.txt”);
//检查错误
if(infle.fail()){
库特格特哈德;
而((GetHardward!=“#”)){
硬[硬计数]=GetHardward;
硬计数++;
填充>>格特哈德;
}

cout您不使用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.
for(inti=0;i<27;i++)//看起来每种类型有27个单词,如果这是错误的,请更改此选项
{
getline(填充,getHardWord,#);
hard[i]=gethardward;
}
//还有其他的困难。

您不使用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.
for(inti=0;i<27;i++)//看起来每种类型有27个单词,如果这是错误的,请更改此选项
{
getline(填充,getHardWord,#);
hard[i]=gethardward;
}
//还有其他的困难。

你是不是把
=
错当成了你的条件句中的
=
?是的,我是@DavidO。感谢大家的输入,尤其是user2599140。你是不是把
=
错当成了你的条件句中的
=
?是的,我是@DavidO。感谢大家的输入,尤其是user2599140。