我正确打开了我的 input.csv
文件,内部有内容。但是,当我cout
我阅读的内容时,它们都是空的。
输入文件是一个问卷反馈,其中第一行和以下相应点上的问题名称。
输入文件的一个示例是:
名称,Q1,Q2
ann,7,6
ben,5,6
int main(){
string input_file;
string output_file;
cout<<"input file name"<<endl;
cin>>input_file;
output_file = input_file.insert(input_file.length()-4, "_out");
ifstream fin(input_file);
if(fin.is_open()){
cout<<"the file name is correct. ";
}
else{
cout<<"the file name does not exist.";
return 0;
}
ofstream fout(output_file);
int sample_size = 0;
int highest_point = 0;
cout<<"What is the highest point possible?"<<endl;
cin>>highest_point;
vector<Tag*> tags;
string first_line;
string tag_name;
getline(fin, first_line);
//as reminded, I added a check here;
if (!std::getline(fin, first_line)) { std::cerr << "Failed to read first linen"; std::exit(1); }
//this does prints out the error message so I guess the getline function is not even working correctly.
//When I cout first_line here, it prints nothing on my terminal
cout<<first_line;
istringstream stringIn1(first_line);
while(getline(stringIn1, tag_name, ',')){
//WHEN I COUT tag_name HERE, IT PRINTS NOTHING ON MY TERMINAL EITHER
cout<<tag_name;
tags.push_back(new Tag(tag_name));
}
此行
output_file = input_file.insert(input_file.length()-4, "_out");
没有做您期望的事情,而是在修改Input_file的名称,以便output_file和input_file相同。因此,打开相同的文件。还始终从getline中查看返回值,这是一个很好的做法。
首先将字符串复制到output_file,然后在output_file上进行插入。