バグが多いので、誰かが私のコードを変更できることを願っています。うまくいくときもあれば、うまくいかないときもある..
もっと説明させてください.. テキストファイルのデータは以下の通りです
Line3D, [70, -120, -3], [-29, 1, 268]
Line3D, [25, -69, -33], [-2, -41, 58]
上記の行を読むには..次を使用します
char buffer[30];
cout << "Please enter filename: ";
cin.ignore();
getline(cin,filename);
readFile.open(filename.c_str());
//if successfully open
if(readFile.is_open())
{
//record counter set to 0
numberOfRecords = 0;
while(readFile.good())
{
//input stream get line by line
readFile.getline(buffer,20,',');
if(strstr(buffer,"Point3D"))
{
Point3D point3d_tmp;
readFile>>point3d_tmp;
// and so on...
次に、Line3d の ifstream でオーバーロードを行いました
ifstream& operator>>(ifstream &input,Line3D &line3d)
{
int x1,y1,z1,x2,y2,z2;
//get x1
input.ignore(2);
input>>x1;
//get y1
input.ignore();
input>>y1;
//get z1
input.ignore();
input>>z1;
//get x2
input.ignore(4);
input>>x2;
//get y2
input.ignore();
input>>y2;
//get z2
input.ignore();
input>>z2;
input.ignore(2);
Point3D pt1(x1,y1,z1);
Point3D pt2(x2,y2,z2);
line3d.setPt1(pt1);
line3d.setPt2(pt2);
line3d.setLength();
}
しかし、問題は、レコードの作業である場合とそうでない場合があることです.. 私が言いたいのは、この時点で
//i add a cout
cout << x1 << y1 << z1;
cout << x2 << y2 << z2;
//its works!
Point3D pt1(x1,y1,z1);
Point3D pt2(x2,y2,z2);
line3d.setPt1(pt1);
line3d.setPt2(pt2);
line3d.setLength();
しかし、カウトを取り除くとうまくいきません。データを適切に処理できるように cin.ignore() を変更するにはどうすればよいですか。数値の範囲は -999 ~ 999 であると考えてください。