文本文件内容
a,b,c,d,
efgh
ijk1
我想存储在数组中,myArray[];预期输出将基于逗号拆分:
myArray[0] = a;
myArray[1] = b;
myArray[2] = c;
myArray[3] = d;
myArray[4] = efgh;
myArray[5] = ijkl;
我做了什么
string myArray[100];
int array_count = 0;
ifstream file((path+dicfile).c_str());
std::string str;
while (std::getline(file, str,','))
{
myArray[array_count] = str; // store those value in array
cout << str << "\n";
strings.push_back(str);
array_count++;
}
我做的输出
myArray[0] = a;
myArray[1] = b;
myArray[2] = c;
myArray[3] = d;
myArray[4] = efghijkl;
最佳答案
以下代码是对原始代码的补充,按行拆分,然后按逗号拆分该行:
string myArray[100];
int array_count = 0;
ifstream file((path+dicfile).c_str());
std::string line;
while (std::getline(file, line))
{
std::istringstream iss(line);
std::string str;
while (std::getline(iss, str, ','))
{
myArray[array_count] = str; // store those value in array
cout << str << "\n";
strings.push_back(str);
array_count++;
}
}
关于c++ - 读取行文本文件,如果有逗号则拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43865943/