samedi 11 février 2023

How to take user input and write it to a pipe?

I just want to take a user input from cin and write it to a pipe but unable to do so.

if(id==0)  
{  
    close(fd[0]);  
    int x;  
    cout<<"Enter a number : ";  
    cin>>x;  
    write(fd[1],&x,sizeof(int));  
    close(fd[1]);  
}  
else  
{  
    close(fd[1]);  
    int y;  
    read(fd[0],&y,sizeof(int));  
    close(fd[0]);  
    cout<<"X is same as Y i.e : "<<y;  
    wait(NULL);     
}

Getting output as:- "Enter a number : X is same as Y i.e ".

But if I remove the close() then it works fine.

Aucun commentaire:

Enregistrer un commentaire