0
商店输入文件我有以下数据的txt文件:C++与多个分隔符
Point2D, [3, 2]
Line3D, [7, 12, 3], [-9, 13, 68]
Point3D, [1, 3, 8]
Line2D, [5, 7], [3, 8]
实际上,我怎么去通过去除多个分隔符,所以我可以提取出来的数据存储它们?
我想要读入第一行并忽略“,”“[”和“]”,所以我可以单独存储Point2D,3和2。然后,我继续进行第二行,等等。
另外,是有可能做到这一点,让说,例如:
第一行 “的Point2D,[3,2]”,被检测时的Point2D时,它将3和2存入的Point2D。 x和point2d.y。
对于第二行“Line3D,[7,12,3],[-9,13,68]”,它会将值存储到line3d.x,line3d.y,line3d.z等等中因此。
现在,我只能得到它忽略“”。这是我到目前为止已经完成:
void readData()
{
string fileName;
int i=0;
cout << "Enter file name: ";
cin >> fileName;
fstream infile;
infile.open(fileName.data());
// This will count the number of lines in the textfile.
if (! infile.is_open())
{
cerr<<"Error : " << fileName.data() <<" is not found"<<endl;
}
string line;
stringstream field;
while (getline(infile,line))
{
string f;
field<<line;
while (getline(field,f,','))
{
recordA.push_back(f);
}
field.clear();
}
cout << recordA.size() << " records read in successfully!";
infile.close();
}
对不起,不是一个差异的人 – user1793001
我在前段时间发布了一个[答案](http://stackoverflow.com/a/2909187/179910)到一个类似的问题。看起来这个基本想法在这里起作用。如果你真的不想明确地读取逗号和括号,你可以创建一个将它们视为空白的语言环境。 [一个例子](http://stackoverflow.com/a/10060244/179910),用斜杠和破折号做。 –
你以前在这里,刚刚创建了一个新账户?这个问题看起来很* *类似于[问题串(http://stackoverflow.com/users/1578897/user1578897?tab=questions),其被要求不久前... –