文件读取和矢量条目问题
Issue with file reading and vector entries
该程序的目的是读取文本文件并将其内容存储在 3 个单独的向量中。
名为 "InsultsSource.txt" 的文本文件包含 50 行以制表符分隔的形容词列,如下所示:
happy sad angry
tired mad hungry
下面是我用来实现此目的的代码。出于某种原因,一切正常,直到第 16 行返回空白。我已经检查了文本文件,看看格式是否发生了变化,但它看起来不错。我只是想知道我的 logic/code 是否有任何错误导致了这个问题。
#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;
}
摆脱 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);
}
您正在阅读 总共 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);
}
该程序的目的是读取文本文件并将其内容存储在 3 个单独的向量中。
名为 "InsultsSource.txt" 的文本文件包含 50 行以制表符分隔的形容词列,如下所示:
happy sad angry
tired mad hungry
下面是我用来实现此目的的代码。出于某种原因,一切正常,直到第 16 行返回空白。我已经检查了文本文件,看看格式是否发生了变化,但它看起来不错。我只是想知道我的 logic/code 是否有任何错误导致了这个问题。
#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;
}
摆脱 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);
}
您正在阅读 总共 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);
}