0

I am trying to

  1. read from scene.txt, do calculations and write to stage1.txt

  2. then read from stage1.txt and write stage2.txt

  3. Finally, read stage2.txt and write stage3.txt;

1 and 2 work just fine. But, I am not quite sure, why can't I do the 3rd?

I have used freopen to redirect stdin and stdout and then before moving from point 1 to 2, I closed both stdin and stdout. Then used freopen with different files again.

I suspect using stringstream causes the problem, but cant say with confidence.

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

I expected the point 3 of above to work. But it did not.

1 Answers1

0

I had to cin.clear() before 3 and after 2. The problem gets solved. But, I do not know why.