【发布时间】:2013-06-05 13:58:25
【问题描述】:
这是我创建的文本文件
产品名称、价格、可用性。
石油,20 美元,是的
油漆,25 美元,是的
CarWax,35 美元,没有
制动液,50 美元,是的
我想从文件中逐行读取这些数据,然后用逗号(,)符号将其拆分并保存在字符串数组中。
string findProduct(string nameOfProduct)
{
string STRING;
ifstream infile;
string jobcharge[10];
infile.open ("partsaval.txt"); //open the file
int x = 0;
while(!infile.eof()) // To get you all the lines.
{
getline(infile,STRING); // Saves the line in STRING.
stringstream ss(STRING);
std::string token;
while(std::getline(ss, token, ','))
{
//std::cout << token << '\n';
}
}
infile.close(); // closing the file for safe handeling if another process wantst to use this file it is avaliable
for(int a= 0 ; a < 10 ; a+=3 )
{
cout << jobcharge[a] << endl;
}
}
问题:
当我删除打印令牌行上的注释时,所有数据都被完美打印,但是当我尝试打印数组的内容(jobcharge[])时,它不会打印任何内容。
【问题讨论】:
标签: c++ arrays codeblocks string-split