C++ C++;:为什么向量实现不产生逗号分隔的值?
我有一个带有逗号分隔值的C++ C++;:为什么向量实现不产生逗号分隔的值?,c++,C++,我有一个带有逗号分隔值的database.txt文件: Name,ID,Year,Gender 我想提取这些元素中的每一个 我从这段代码开始(我已经研究了所有其他类似的问题,并实现了他们的建议),但它并没有打印每一条: // reading a text file #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstr
database.txt
文件:
Name,ID,Year,Gender
我想提取这些元素中的每一个
我从这段代码开始(我已经研究了所有其他类似的问题,并实现了他们的建议),但它并没有打印每一条:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main () {
string line;
ifstream myfile ("database.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
std::string str = line;
std::vector<int> vect;
std::stringstream ss(str);
int i;
while (ss >> i)
{
vect.push_back(i);
if (ss.peek() == ',')
ss.ignore();
}
for (i=0; i< vect.size(); i++)
std::cout << vect.at(i)<<std::endl;
//cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
//读取文本文件
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
弦线;
ifstream myfile(“database.txt”);
如果(myfile.is_open())
{
while(getline(myfile,line))
{
std::string str=line;
std::vector-vect;
std::stringstream ss(str);
int i;
while(ss>>i)
{
向量推回(i);
如果(ss.peek()==',')
忽略();
}
对于(i=0;i std::cout使用此函数拆分每行:
vector<string> split(const string &s, char delim) {
stringstream ss(s);
string item;
vector<string> tokens;
while (getline(ss, item, delim)) {
tokens.push_back(item);
}
return tokens;
}
向量拆分(常量字符串&s,字符delim){
溪流ss(s);;
字符串项;
向量标记;
while(getline(ss、item、delim)){
代币。推回(物品);
}
归还代币;
}
您的代码应该是:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main () {
string line;
ifstream myfile ("database.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
std::string str = line;
std::vector<string> vect;
vect = split(str, ',') ;
for (int i=0; i< vect.size(); i++)
std::cout << vect.at(i)<<std::endl;
//cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
//读取文本文件
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
弦线;
ifstream myfile(“database.txt”);
如果(myfile.is_open())
{
while(getline(myfile,line))
{
std::string str=line;
std::vector-vect;
vect=分裂(str,,);
对于(int i=0;i std::cout借助一个实用函数和一个数据结构,您可以很容易地简化这个过程
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
std::vector<std::string> splitString( const std::string& stringToSplit, const std::string& delimiter, const bool keepEmpty ) {
std::vector<std::string> results;
if ( delimiter.empty() {
results.push_back( stringToSplit );
return results;
}
std::string::const_iterator itSubStrStart = stringToSplit.begin(), itSubStrEnd;
while( true ) {
itSubStrEnd = std::search( itSubStrStart, stringToSplit.end(), delimiter.begin(), delimiter.end() );
std::string temp( itSubStrStart, itSubStrEnd );
if ( keepEmpty || !temp.empty() )
results.push_back( temp );
if ( itSubStrEnd == stringToSplit.end() )
break;
itSubStrStart = itSubStrEnd + delimiter.size();
}
return results;
}
struct DataEntry {
std::string name;
unsigned id;
unsigned year;
std::string gender;
};
int main() {
std::string line;
std::fstream file;
file.open( "database.txt" );
std::vector<DataEntry> entries;
std::vector<std::string> elements;
while( file >> line ) {
elements = splitString( line, "," );
DataEntry entry;
entry.name = elements[0];
entry.id = std::stoul( elements[1] );
entry.year = std::stoul( elements[2] );
entry.gender = elements[3];
entries.push_back( entry );
}
file.close();
for ( auto& e : entries ) {
std::cout << e.name << " " << e.id << " "
<< e.year << " " << e.gender << '\n';
}
std::cout << "\nPress any key and enter to quit.\n";
std::cin.get();
return 0;
}
输出
这使我们的生活变得更加简单,只需先阅读一行文字,然后解析这行文字,然后再对这些数据进行处理;或者将其存储到结构中,或者打印它,或者对其进行操作等等
编辑
您需要意识到这样一个事实,即当读取一行行文本并对其进行解析时,如果您的文本文件中有类似的内容:
John Doe,12345,2010,M
它不会给你你想要的。我会留给你去弄清楚。这不太可能与向量实现有任何关系。你是否尝试过用调试器单步遍历代码,看看它实际做了什么,而不是你想要它做什么?如果这行std::string str=line;是replac,这将起作用用std::string str=“a,b,c,d”编辑,这意味着它将打印a、b、c、d,这让我觉得这与行的格式有关。我只是不知道行可能有什么问题?字符串格式最好先按“,”拆分行,然后将每个行推回到向量。为什么要从流中读取int值并将它们放入一个包含int的向量中?如果INE包含名称和性别,那么你难道不想从流中读取字符串而不是int值吗?这很有效!非常感谢@amin saffar,虽然我错认为向量有效,但它应该是向量,而int没有声明,所以我只是编辑了你的答案以反映这些更改,谢谢!上面的工作很好;但它不正确t从函数中读取文件的部分,只是解析一个文本字符串。解析从文件中读入的实际字符串应该独立于从文件中读取一行字符串的实际读数。
John 12345 2010 M
Jane 54321 2012 F
John Doe,12345,2010,M