run program with perl or C++ - c++

I am looking for a way to grade C++ programs.
I want to be able to execute numerous programs that will have a standard input/output path, so each program will be required to output and input information at the appropriate time.
What is the best way to do this? I have seen that some people say write a C++ program that calls fork() but my question about that is how do you interact with the same input and output screen? (in other words, once you fork that process, can you feed that process input and view it's output?) Would a perl script work better?
If so, can you give me a few lines of the perl script and what it would look like.

You should use the Posix popen or open3 to open the program and interract with its stdin and stdout. You should be able to do this in any language; personally I would use Ruby and rspec to do the grading.
http://pubs.opengroup.org/onlinepubs/009604499/functions/popen.html
Be sure to run the students' code as a user with restricted permissions and use a chroot jail!

Related

Use User Input/Text File as Code

Is it possible to treat user input as lines of code in C++?
Or write it to a text file, then run that as c++ code?
I'm writing my own version of matlab, and right now it can do a lot of the numerical stuff, but only if I write the commands in like a script in main. Is there a way I could do it like matlab's command line?
Yes, there are many libraries that allow for scripting like Lua or Chaiscript which will allow you to take a file of code and execute it. There's also the advantage of being able to make changes to the code and not having to recompile each time.

Load file into input stream and use scanf()

I am very new to c++.
I read some code samples of some experienced c++ coders, and noticed that they ran a file starting with scanf, that read through a large input.
However, they didn't use any specified ifstream to read in a file, they seemed to just run scanf as if this entire input .txt file was already in the stream.
Is this because they are using some special IDE where you can load in input file into the standard stream?
I am simply trying to run their code to duplicate something they are doing, but I need some way to read in a 5000+ line text file and then run scanf on it.
How do I do this. I'm not using any sort of IDE, just notepad++. If someone recommends an IDE, i'll take it, but otherwise would just like advice on how to do this. Thanks.
For a start, if they're still using scanf(), they are not experienced C++ programmers. If they were experienced, they would be using the C++ I/O facilities rather than the legacy C ones.
Having said that, you can quite easily use the functions accepting standard input (either scanf() or cin) if you can do redirection on the program, with lines like:
myProg <myInputFile.txt
someOtherProgWhichProducesOutput | myProg
This "attaches" the file myInputFile.txt to the standard input of the myProg program, or takes the output from another program and feeds it to the input of myProg, and is what makes UNIX and its brethren (including Windows in this context, since it too has redirection and pipelines) so powerful, allowing you to string together complicated pipelines from relatively simple commands.

Reading output from the command line

I'm trying to write a simple GUI for Wget. I'm looking for advice on how to read information from the command line output that Wget generates when it is doing a run. I'd like to update that download information real time to a list box or some equivalent. The GUI will be in Visual Basic. I know programs like WinWget do this, and their source code is available, but I don't know the language that's written in well enough to find what I'm looking for.
tl;dr: I need to update a list box real time with command line output.
There are two ways to use the output of one console application for the input of an other:
The first way is to use the | operator; for example:
dir |more
The second way is to write the data into a file and process it later.
dir > data.txt

Simple interactive prompt in C++

I work on an application that usually runs unattended. Now I need to add to it something like an interactive prompt. In the interactive mode the operator will be able to give simple commands to the application - nothing fancy, simple commands like start and stop. Parametrized commands (e.g. repeat 10) and commands history could be nice too.
Do you know, by chance, any library that helps with such tasks. I've been thinking about something that works like boost::program_options or gflags but for an interactive prompt and not for command line parameters. Any ideas?
Thanks
Readline is one the best known libraries for this
http://tiswww.case.edu/php/chet/readline/rltop.html
It is covered by GPL, so it is only possible to use in GPL-compatible programs.
I did a quick search for alternatives, and found this:
http://github.com/antirez/linenoise
I'm not sure if the following is a reasonable amount of work for what you're trying to do, but Python has a very nice Command Line Interface (CLI) building library called cmd2. If it's possible to expose the relevant parts of your apps to Python using SWIG or CTypes, then doing the rest should be easy.
Here's a nice video presentation about cmd2:
PyCon 2010:Easy command-line applications with cmd and cmd2
HTH
One possibilty is to open a TCP port and accept messages in text format. Then you can telnet to that port and issue simple commands.

Program Interaction and testing via bash script

I've just completed the coding section of simple homework assignment for my C++ class. The second part of the assignment requires us to verify our code's input validation. (The program takes several different values as inputs from a user and prints those values to a file)
I was hoping that I could use bash script for this. Is there any way to use bash script to run and interact with a program? How can I put the program's output into a variable (note that program has a series of input requests and outputs).
Thanks
To build on #Travis' answer, create two files: one holds your inputs (input.txt) and one holds the expected output (expected_output.txt). Then do the following:
./myprogram <input.txt >output.txt
diff output.txt expected_output.txt
If the diff command has any output, there's a problem.
You can do much of this with a shell script but you might want to consider using some other testing tools instead like CppUnit or expect.