C++ 文件读取和向量项问题

C++ 文件读取和向量项问题,c++,vector,readfile,C++,Vector,Readfile,该程序的目的是读取文本文件并将其内容存储在3个单独的向量中 名为“侮辱资源.txt”的文本文件包含50行以制表符分隔的形容词列,如下所示: happy sad angry tired mad hungry 下面是我用来实现这一点的代码。出于某种原因,在返回空白的第16行之前,“一切”都有效。我检查了文本文件,看看周围的格式是否有变化,但看起来不错。我只是想知道我的逻辑/代码中是否有导致此问题的错误 #include <vector> #include <

该程序的目的是读取文本文件并将其内容存储在3个单独的向量中

名为“侮辱资源.txt”的文本文件包含50行以制表符分隔的形容词列,如下所示:

happy    sad    angry
tired    mad    hungry
下面是我用来实现这一点的代码。出于某种原因,在返回空白的第16行之前,“一切”都有效。我检查了文本文件,看看周围的格式是否有变化,但看起来不错。我只是想知道我的逻辑/代码中是否有导致此问题的错误

#include <vector>
#include <string>
#include <fstream>
#include <iostream>

using namespace std;

int main() {

    ifstream fileIn("InsultsSource.txt");
    vector<string> col1;
    vector<string> col2;
    vector<string> col3;
    string word;

    if (fileIn.fail()) {
        cerr << "Unable to open file" << endl;
    }

    for (int i = 0; i < 50; i++) {
        if (i % 3 == 0) {
            getline(fileIn, word, '\t');
            col1.push_back(word);
        }
        else if (i % 3 == 1) {
            getline(fileIn, word, '\t');
            col2.push_back(word);
        }
        else {
            getline(fileIn, word);
            col3.push_back(word);
        }
    }

    for(int j = 0; j < 50; j++) {
        cout << j+1 << " " << col1[j] << endl;
        //cout << "Thou " << col1[j] << " " << col2[j] << " " << col3[j] << "!" << endl;
    }
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream fileIn(“InsultsSource.txt”);
载体col1;
载体col2;
载体col3;
字符串字;
if(fileIn.fail()){

cerr摆脱
for
循环,改用
while

std::string text;
while (std::getline(fileIn, text, '\t'))
{
  col1.push_back(text);
  std::getline(fileIn, text, '\t');
  col2.push_back(text);
  std::getline(fileIn, text);
  col3.push_back(text);
}
在这种情况下,您可能希望使用结构对每条线进行建模

struct Record
{
  std::string col1;
  std::string col2;
  std::string col3;
}
std::vector<Record> database;
Record r;
while (std::getline(fileIn, r.col1, '\t')
{
  std::getline(fileIn, r.col2, '\t');
  std::getline(fileIn, r.col3);
  database.push_back(r);
}
struct记录
{
std::字符串col1;
std::字符串col2;
std::字符串col3;
}
向量数据库;
记录r;
while(std::getline(fileIn,r.col1,“\t”)
{
std::getline(fileIn,r.col2,'\t');
std::getline(fileIn,r.col3);
数据库。推回(r);
}

摆脱
for
循环,改用
while

std::string text;
while (std::getline(fileIn, text, '\t'))
{
  col1.push_back(text);
  std::getline(fileIn, text, '\t');
  col2.push_back(text);
  std::getline(fileIn, text);
  col3.push_back(text);
}
在这种情况下,您可能希望使用结构对每条线进行建模

struct Record
{
  std::string col1;
  std::string col2;
  std::string col3;
}
std::vector<Record> database;
Record r;
while (std::getline(fileIn, r.col1, '\t')
{
  std::getline(fileIn, r.col2, '\t');
  std::getline(fileIn, r.col3);
  database.push_back(r);
}
struct记录
{
std::字符串col1;
std::字符串col2;
std::字符串col3;
}
向量数据库;
记录r;
while(std::getline(fileIn,r.col1,“\t”)
{
std::getline(fileIn,r.col2,'\t');
std::getline(fileIn,r.col3);
数据库。推回(r);
}

您总共阅读了50个单词,然后尝试打印每列中的50个单词。

您总共阅读了50个单词,然后尝试打印每列中的50个单词。

请使用以下方法

std::string val1, val2; val3;

vector<string> col1;
vector<string> col2;
vector<string> col3;

while(fileIn >> val1 >> val2 >> val3) {
    col1.push_back(val1);
    col2.push_back(val2);
    col3.push_back(val3);
}
std::字符串val1,val2;val3;
载体col1;
载体col2;
载体col3;
而(fileIn>>val1>>val2>>val3){
col1.推回(val1);
col2.推回(val2);
col3.推回(val3);
}

而是使用类似

std::string val1, val2; val3;

vector<string> col1;
vector<string> col2;
vector<string> col3;

while(fileIn >> val1 >> val2 >> val3) {
    col1.push_back(val1);
    col2.push_back(val2);
    col3.push_back(val3);
}
std::字符串val1,val2;val3;
载体col1;
载体col2;
载体col3;
而(fileIn>>val1>>val2>>val3){
col1.推回(val1);
col2.推回(val2);
col3.推回(val3);
}

假设文件中有50个条目。更好的算法是在文件中有数据时读取。第二个
for
循环(包含
j
cout
)导致崩溃,因为你正在迭代通过数组边界?@ThomasMatthews你是对的。我只是想,因为我只是想让它工作,而且我知道有50行,这不会有什么坏处。@UnholySheep每列应该有50个条目,所以我不确定你为什么认为它会崩溃。你看到通过该快捷方式节省了多少时间吗?你的屁股读取文件中的50个条目。更好的算法是在文件中有数据时读取。第二个
for
循环(包含
j
cout
)不是吗导致崩溃,因为你正在迭代通过数组边界?@ThomasMatthews你是对的。我只是想,因为我只是想让它工作,而且我知道有50行,这不会有什么坏处。@UnholySheep每列应该有50个条目,所以我不确定你为什么认为它会崩溃。你看到通过这个快捷方式节省了多少时间吗?哇,我知道非常感谢你的评论。我知道我的代码不是最优雅的,但我一直在寻找改进的方法。哇,我非常感谢你的评论。我知道我的代码不是最优雅的,但我一直在寻找改进的方法。是的,那是最棒的。谢谢一群人,帮我省去了很多麻烦。是的,那是最棒的。谢谢一群人安,帮我省去了很多头痛。