我想创建一个程序,它将加载CSV文件,并根据输入的文字搜索文件并返回任何包含该单词的行。 CSV文件是微博的海量下载,并具有以下栏目:C++从输入的字符串搜索CSV文件
- 日期&创建时间
- 鸣叫
的微博也被b'TWEET TEXT HERE”,让周围会需要从打印出来时删除b“。我无法改变任何与CSV文件可悲,所以不能手动删除它。我遇到的问题是:
- 清单的程序只是冻结
- 去除
- else语句导致‘未找到’的鸣叫B”“的文件内鸣叫的总量是不断印刷
代码我目前已经返回包含输入词的推文,也包含误报。
电流输出运行下面的代码时
#include "stdafx.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string token;
ifstream fin;
fin.open("sampleTweets.csv");
if (fin.is_open())
{
cout << "File opened successfully" << "\n";
}
else {
cout << "Error opening file" << "\n";
}
cout << "Enter search word: ";
cin >> token;
"\n";
string line;
while (getline(fin, line)) {
if (line.find(token) != string::npos) {
cout << line << endl;
} else {
cout << token << " not found" << endl;
}
}
fin.close();
char anykey;
cout << "press any key";
cin >> anykey;
return 0;
}
我所用,用于计算总鸣叫
int count = 0;
char str[140];
while (!fin.eof())
{
fin.getline(str, 140);
count++;
}
cout << "Number of lines in file are " << count;
代码任何帮助将是我相当惊人新的C++和不确定从哪里去!
为什么你无条件地运行'cout << token <<“not found”<< endl;'? – NathanOliver
啊,这将解释它总是打印出来的事实... –
cin >> token; “\ n” 个;我不确定你在这里想达到什么,但这是错误的。 – willll