使用fstreams和stringstreams:获取字符串后获取字符时遇到问题(C ++)

时间:2015-03-04 23:46:10

标签: c++ output fstream stringstream

(C ++)我想显示第一部分,它工作正常,然后美元符号在另一条线上,然后是价格。这就是我所拥有的:

string mystring;
ofstream oFile( "C:\\beer.txt" );
oFile << "Heineken#$7.99" << endl;

fstream File( "C:\\beer.txt" , ios :: in | ios :: out );
getline(File, mystring, '#');
stringstream mystringstream;
mystringstream.str(mystring);
cout << "The list contains: " << mystring << endl;

/////////////////////////////////////////////////////////////////
// Everything above this line works just fine
// now I want to display the dollar sign

string mystring2;
getline (File, mystring2, '7');
stringstream mystringstream2;
mystringstream2.str(mystring2);
char dollar = mystring.at(mystring2.length()-1);
cout << "The tender is: " << dollar << endl ;
// this does not work

///////////////////////////////////////////////////////////
//now, I want to display the price, in xx.xx format
//I've been looking for proper functions, but nothing works. Any help 
//is greatly appreciated.

1 个答案:

答案 0 :(得分:0)

对于第二部分,这应显示金额:

string mystring2;
getline (File, mystring2);
std::cout << "Amount: " << mystring2 << endl ;

您已经使用第一个getline调用读取了金额之前的部分,因此下一个getline将读取字符串的其余部分。只需打印出来。