Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

child process in node.js

I am trying to compile a c++ file and produce output using node and match it with the answer file that I have. And find the diff between my answer.txt and output.txt , in terminal when I run those command it shows me the line where they both differed 1st time , but when i run through node if both file are same I dont get anything same as my terminal , but when my output.txt file does not matches with answer.txt file I get an Error saying command failed using node , and line where they differed first time in my terminal .

 exec(
        `cd ${dirprob} && g++ ${job.filepath} -o testprogram && ./testprogram <input.txt >output.txt && diff output.txt answer.txt ` , 
        
        (error,stdout , stderr)=>{
            if (error) {
                
                return ;
            }
            if (stderr) {
                console.log(`stderr: ${stderr}`);
                return ;
            }
            console.log(`stdout: ${stdout}`);
            return ;
        })

above is the code of my exec function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are misunderstanding what error means in exec. It is the return code of the program you ran, and diff will return a non-zero code when the files are not equal. So this is not an error in the sense that the command failed. But in your code you are bailing when it happens.

Just remove the if (error) { return; }.

Btw, you should probably break down the long exec command string, too, and use cwd to set the working directory rather than cding. Ideally you’d have the diff call be completely separate so that you can be sure that errors (that I propose to ignore) are indeed coming from that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading