尝试读入 CSV 文件并打印到控制台 C++
Trying to read in a CSV file and print to console c++
我已经有一段时间没有编程了,正在尝试从事一个项目,该项目将从 csv 文件中读取推文,然后操作存储的内容。目前,我正在尝试从文件中提取数据并打印到控制台。我知道我的文件正在打开,因为我包含了一个条件语句,但是,当涉及到读取数据而不是获取任何实际信息时,我只是得到空行。我还认为这可能是因为我正在使用的 csv 文件非常大(20k 数据条目)所以我添加了一个 for 循环来尝试找出问题。
我正在使用 getline 和 stringstream 来获取数据,并使用 cout 打印到控制台。我似乎找不到问题所在。
这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string line;
ifstream fin;
fin.open("train_dataset_20k.csv");
if (!fin.is_open()){
cout << "Error opening file" << endl;
return 1;
}
while(fin.is_open()){
for (int i = 0; i <10; i ++){
string Sentiment,id,Date,Query,User,Tweet;
stringstream lineSS(line);
getline(lineSS, Sentiment, ',');
getline(lineSS, id, ',');
getline(lineSS, Date, ',');
getline(lineSS, Query, ',');
getline(lineSS, User, ',');
getline(lineSS, Tweet, '\n');
cout << Sentiment << endl;
cout << id << endl;
cout << Tweet << endl;
}
fin.close();
}
return 0;
}
目前,它会运行 for循环10次,但只会输出没有任何信息的空行。
I know that my file is opening because I had included a conditional statement, however, when it comes to reading the data rather than getting any actual information I am just getting blank lines.
您永远不会在代码中的任何地方初始化 line
变量,因此 lineSS
是用空缓冲区初始化的。
因此,使用 std::getline()
读取具有空缓冲区的流将给您一个空字符串,这就是您所面临的。
要解决此问题,请将 std::getline(fin, line);
放在第二个循环的开头:
// ...
if (fin.is_open()) {
for (int i = 0; i < 10; i ++) {
getline(fin, line); // Reads each line from the file into the 'line' variable
string Sentiment, id, Date, Query, User, Tweet;
stringstream lineSS(line);
getline(lineSS, Sentiment, ',');
getline(lineSS, id, ',');
getline(lineSS, Date, ',');
getline(lineSS, Query, ',');
getline(lineSS, User, ',');
getline(lineSS, Tweet, '\n');
cout << Sentiment << endl;
cout << id << endl;
cout << Tweet << endl;
}
fin.close();
}
// ...
或者直接使用文件流而无需使用std::stringstream
进行调解:
// ...
if (fin.is_open()) {
for (int i = 0; i < 10; i ++) {
string Sentiment, id, Date, Query, User, Tweet;
getline(fin, Sentiment, ',');
getline(fin, id, ',');
getline(fin, Date, ',');
getline(fin, Query, ',');
getline(fin, User, ',');
getline(fin, Tweet, '\n');
cout << Sentiment << endl;
cout << id << endl;
cout << Tweet << endl;
}
fin.close();
}
// ...
我已经有一段时间没有编程了,正在尝试从事一个项目,该项目将从 csv 文件中读取推文,然后操作存储的内容。目前,我正在尝试从文件中提取数据并打印到控制台。我知道我的文件正在打开,因为我包含了一个条件语句,但是,当涉及到读取数据而不是获取任何实际信息时,我只是得到空行。我还认为这可能是因为我正在使用的 csv 文件非常大(20k 数据条目)所以我添加了一个 for 循环来尝试找出问题。
我正在使用 getline 和 stringstream 来获取数据,并使用 cout 打印到控制台。我似乎找不到问题所在。
这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string line;
ifstream fin;
fin.open("train_dataset_20k.csv");
if (!fin.is_open()){
cout << "Error opening file" << endl;
return 1;
}
while(fin.is_open()){
for (int i = 0; i <10; i ++){
string Sentiment,id,Date,Query,User,Tweet;
stringstream lineSS(line);
getline(lineSS, Sentiment, ',');
getline(lineSS, id, ',');
getline(lineSS, Date, ',');
getline(lineSS, Query, ',');
getline(lineSS, User, ',');
getline(lineSS, Tweet, '\n');
cout << Sentiment << endl;
cout << id << endl;
cout << Tweet << endl;
}
fin.close();
}
return 0;
}
目前,它会运行 for循环10次,但只会输出没有任何信息的空行。
I know that my file is opening because I had included a conditional statement, however, when it comes to reading the data rather than getting any actual information I am just getting blank lines.
您永远不会在代码中的任何地方初始化 line
变量,因此 lineSS
是用空缓冲区初始化的。
因此,使用 std::getline()
读取具有空缓冲区的流将给您一个空字符串,这就是您所面临的。
要解决此问题,请将 std::getline(fin, line);
放在第二个循环的开头:
// ...
if (fin.is_open()) {
for (int i = 0; i < 10; i ++) {
getline(fin, line); // Reads each line from the file into the 'line' variable
string Sentiment, id, Date, Query, User, Tweet;
stringstream lineSS(line);
getline(lineSS, Sentiment, ',');
getline(lineSS, id, ',');
getline(lineSS, Date, ',');
getline(lineSS, Query, ',');
getline(lineSS, User, ',');
getline(lineSS, Tweet, '\n');
cout << Sentiment << endl;
cout << id << endl;
cout << Tweet << endl;
}
fin.close();
}
// ...
或者直接使用文件流而无需使用std::stringstream
进行调解:
// ...
if (fin.is_open()) {
for (int i = 0; i < 10; i ++) {
string Sentiment, id, Date, Query, User, Tweet;
getline(fin, Sentiment, ',');
getline(fin, id, ',');
getline(fin, Date, ',');
getline(fin, Query, ',');
getline(fin, User, ',');
getline(fin, Tweet, '\n');
cout << Sentiment << endl;
cout << id << endl;
cout << Tweet << endl;
}
fin.close();
}
// ...