Thursday, August 18, 2022

[FIXED] How to read output of source code in another program with C++

Issue

I think this is really hard to explain what I want. But, let me try. (I am trying to build a program for scoring student's programming homework)

There are a lot of simple source codes in C++. (Please think there are more than 100 code files)

// C:\homework1\studentA.cpp
int main()
{
    cout << "The answer is 456" << endl;
}

And This is question. As you can see, there are tons of code files and I cannot compile it and check it whether right or wrong one by one. So, I need to make scoring program for convenience.

How can I read the standard output (The answer is 456) in another program? Is there any function for 'compiling source code' and 'save standard output' ?


Solution

I would use a bash script for this instead of C++. Something along the lines of:

g++ $filename
./a.out > student_answer.txt
diff -q student_answer.txt expected_answer.txt

Then, $? would tell you whether the answer was correct.



Answered By - Daniel
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.