0

我在尝试着

  1. 从 scene.txt 中读取,进行计算并写入 stage1.txt

  2. 然后从 stage1.txt 读取并写入 stage2.txt

  3. 最后,读取stage2.txt,写入stage3.txt;

1和2工作得很好。但是,我不太确定,为什么我不能做第三个?

我使用 freopen 重定向标准输入和标准输出,然后在从第 1 点移动到第 2 点之前,我关闭了标准输入和标准输出。然后再次将 freopen 与不同的文件一起使用。

我怀疑使用 stringstream 会导致问题,但不能自信地说。

freopen("scene.txt","r",stdin);
freopen("stage1.txt","w",stdout);

//works fine. writes to stage1.txt

fclose(stdin);
fclose(stdout);
freopen("stage1.txt","r",stdin);
freopen("stage2.txt","w",stdout);
string test;
while(getline(cin,test))
{
    if(test=="")
    {
        cout<<endl;
        continue;
    }

    point p(1);
    stringstream s(test);

    s>>p.matrix[0]>>p.matrix[1]>>p.matrix[2];
    point res;
    res=apply_transformation(v,p);
    res.print();
}

//it also does read from stage1.txt and writes to stage2.txt


fclose(stdout);
fclose(stdin);
freopen("stage2.txt","r",stdin);
freopen("stage3.txt","w",stdout);

string test3;
while(getline(cin,test3))
{
    cout<<"YES"<<endl; //never gets here. cant even read stage2.txt
    if(test3=="")
    {
        cout<<endl;
        continue;
    }

    point p(1);
    stringstream s(test3);

    s>>p.matrix[0]>>p.matrix[1]>>p.matrix[2];
    point res;
    res=apply_transformation(P,p);
    res.print();
    cout<<p.matrix[0]<<" A "<<p.matrix[1]<<" "<<p.matrix[2]<<endl;

    //cout<<test<<endl;
}

// above-mentioned loop doesn't work. cant read from stage2 and doesn't write anything to stage3.txt

我希望上面的第 3 点有效。但它没有。

4

1 回答 1

0

我必须在 3 之前和 2 之后 cin.clear() 。问题得到解决。但是,我不知道为什么。

于 2019-07-08T18:06:15.233 回答