该程序没有产生输出,而是卡在一个循环中,几秒钟后消失并打印被杀死?


void show_password(){
int i = 0;
fstream fin;
fin.open("pass_words",ios::in);
while(i == 0){
if(!fin.is_open()){
cerr<<"File not Foundn";
}
else{
break;
}
}
vector<string>data1;
string content;
while(getline(fin,content)){
stringstream f(content);
string words;
while(f,words,','){
data1.push_back(words);
}
}
cout<<data1.size()<<'n';

int y;
for (int i = 0;i < data1.size();++i){
cout<<"-"<<data1[y]<<'n';
++y;
}
}
int main()
{
show_password();
}

我希望这段代码读取"pass_words"CSV 文件并打印其中的数据......但它卡在我不知道的循环中....终端几秒钟没有显示任何内容,突然终端打印"已杀"....我不知道为什么。。。仅供参考,"pas_sword"包含:

hello,facebook
byebye,youtube
helloworld,instagram

终端输出:

Killed

我正在使用 Ubuntu 终端....

目前尚不清楚您希望这段代码做什么:

stringstream f(content);
string words;
while(f,words,','){
data1.push_back(words);
}

逗号运算符(来自 cpp首选项(:

在逗号表达式E1, E2中,表达式E1被计算,其结果被丢弃(尽管如果它具有类类型,则在包含完整表达式的末尾之前不会销毁(,并且在表达式E2的计算开始之前完成其副作用(请注意,用户定义的运算符不能保证排序((直到C++17(。

简单来说:while(f,words,',')while(',')相同,并且由于','的值与零不同,因此与while(true)相同。您将空字符串推入向量,直到该循环无穷大。不确定代码的逻辑,但例如,这会从f中提取逗号分隔的单词并将它们推入data1

stringstream f(content);
string words;
while(std::getline(f,words,',')){
data1.push_back(words);
}

最新更新