Display matlab's program's output calling for another c++ program - c++

I'm not sure if my question is clear:
I have a C++ project that I run from Qtcreator, this program call another c++ script outside the project. This last one runs a shell script calling a bunch of matlab scripts.
I want to display output from the matlab scripts, disp doesn't work.
I tried to write the values I want to read in a .txt file. The file is created but stay empty.
I tried these lines to write in a file:
fileId= fopen('imagename.txt','a');
fprintf(fileId, 'test : %s',imageName);
fclose(fileId);
I also tried assignin with the values I want to show but they're not kept within matlab's workspace.
I can't change the architecture of the whole program because it's a big project made by someone else I have to continue.
Do you have another way to watch what is going on the matlab scripts?
It's difficult to launch them directly from Matlab as I don't have access to their inputs values. I can hardly change the c++ script calling them to display theses values because I have a 'reference to ofstream is ambiguous' issue when I try to build it, so I will have to debug something made by someone else and non commented.

The fact that the file you write to stays empty is weird and I would try to find the reason regardless of your question.
Anyway, since you call Matlab from a shell script I guess you use 'matlab -r' option with your script name, or something similar. In that case you can use the -f option which tells Matlab to write the command line output to a log file:
matlab -logfile output.log

Related

Is there a way to stop RGui from crashing when an RCPP program fails to work correctly?

I'm using Rcpp to run C++ code using RGui (version 3.4.1) as a user interface. Quite often I make changes to the C++ code which compile correctly but cause errors (e.g. searching beyond the end of an array) when I run the relevant program in RGui, causing RGui to crash. This is aggravating because I have to re-open RGui, re-open my R script (sometimes with unsaved changes lost), set the working directory again, etc. before I can re-compile the C++ code and run the program in such a way as to find the problem or test amendments. Sometimes it promptly crashes again because I haven't fixed or bypassed the problem.
Is there some way to change the way Rcpp runs such that RGui returns an error message instead of crashing in these sorts of situations?
Briefly:
It is spelled Rcpp. Capital R, lowercase cpp.
Yes, don't have bugs :)
In general, 2. is the only viable answer. If you need a managed language, use R.
If the code takes your environment down, test outside the environment. Seriously. That is for example why I (co-)wrote littler and test "raw code" on the command-line: it can only take the command-line app down.
We do have a feature in eg RcppArmadillo to test for "out of bounds" vector access: use x.at(i,j) which will warn. See http://arma.sourceforge.net/docs.html#element_access
I don't actually know of a way to prevent this apart from more careful programming, and saving before execution. But having done this a few times I have discovered a way to get back at unsaved changes, (at least in windows).
When you get the pop-up that tells you to restart R, you don't do it. You open up task manager and right-click on the process and select 'Create Dump File'. Find this file in explorer and open it with some text editor.
They are very big, and full of all sorts of stuff, but if you use find function to search for some string you know to be in your script, then you can find all the unsaved work. You can then copy and paste this into another file to save.
If you use R-studio instead of R-GUI, it usually manages to look after your unsaved work better.

fortran: type "executable #script" in a terminal to run program using script

I have a fortran program in which I usually use scripts of commands, for example, in a terminal I write:
$program
then I enter into the program (the terminal shows "$program>") and accepts either commands or a script that I call by typing manually "#script":
$program>#script
then the fortran program opens the file named "script", which contains a series of commands or tasks that are executed.
What I want to do now is to type directly in a terminal:
$program #script
to run the program with the commands contained in the file "script".
I want to do this to be able to create sh scripts to run the program many times without having to enter into the program each time to write manually the name of each script.
Does anybody know how could I do this in fortran. I guess that the way is to start the fortran program by saying that if something was typed in a terminal after the name of the program, then the fortran program should be able to read it and use it internally. No matter which type of variable is given after typing "program", the program should be able to read it directly from the terminal. Any idea will be greatly appreciated.
Thanks in advance.
You may be able to do this:
program <<EOF
#script
EOF
Details may depend slightly on which shell you're scripting in, and on the details of the program you're trying to run.
Also, this is a shell scripting question, not much related to Fortran.
Fortran 2003 defines some intrinsics that allow you to retrieve the command line arguments provided to your executable.
The functions you are interested in are get_command_argument, command_argument_count and get_command.
get_command retrieves the entire command line.
command_argument_count returns an integer with the number of arguments passed on the command line.
get_command_argument gets the nth argument passed on the command line.
Note that this is functionality you'll need to add to your Fortran program, and if you do not have the ability to recompile it, then you cannot make this work using this approach.
In the case modifying the Fortran is not possible, then you may use programs such as expect which let you automate input into programs that provide prompting.

How to generate and compile C++ code while the program is running?

So basically what I need isn't a specific code (of course that would be great), but just an idea and methods on how to achieve my goal.
1) I have to create a program in C++ , which generates a little example of C++ code, that is each time a bit different. (This causes no problems for me, I will use a template and randomize some variables in the code, which will make it unique every time.)
2) I will display the generated code and the user will have to type in, what he thinks the code prints out.
And here is where the problems start:
3) I have to take the generated code and compile it somehow to get a string with the text that the program would have printed out.
4) And then compare the string with what the user has typed in.
So the step 3) is where I stop and can't figure it out without help... I was thinking to write the generated code in a function of a .cpp file and then call that function, but I couldn't get it to work, so I started to think, I should ask an expert, maybe there are some other methods or ideas how to achieve this.
Write the .cpp file
invoke the compiler (or make or equivalent build system)
if compilation fails, presumably display the errors?
if compilation succeeds, run the resulting program and display the output
It's probably simplest to wrap 2/3/4 into a script, and invoke that with system or popen. The script can make sure the filenames are unique, fold stderr into stdout, etc. etc.
Your running program isn't really interacting with the compiled code, just reading the output, so keeping it as a seperate process is probably easiest. The script can add some markup to help you distinguish compiler output/errors from the program output.
I haven't written a batch file for years, but once you know how to run your compiler from the command line (ref), you can write a script to:
compile a .cpp file
execute the resulting .exe
redirect its output to a file
then, in C++ you just need to save the code to the .cpp file the script expects, execute the script like system("myScript.bat"), and then read the output file.
If you don't want to write a seperate batch script, you can just call system once to invoke the compiler, and again to execute the resulting .exe.
I think you are looking for a way to script c++. Have a look at http://www.softintegration.com/ or this stackoverflow question Any tutorial for embedding Clang as script interpreter into C++ Code?.
I have not used this but it is free and also does not compile but interpret. Very suitable for your problem, worth a shot...
http://root.cern.ch/drupal/content/cint
why dont you compile the generated code standalone using system() call?
system("g++ temp.cpp -o temp.exe); --something of this sort and then based on the return value
you can run temp.exe again like system("temp.exe"); Ofcourse you can print the output from temp.exe to a file and read that file to get the output of temp.exe to your current program..
Would something like Geordi work?
You can invoke the c++ compiler just like you'd invoke any external tool. E.g. system("g++ test.cpp") or use popen or whatever else is offered by your platform.
You could also look into integrating a library that implements a compiler into your program. For that you might want to look into clang and llvm.

Writing to Console from DLL

I have a DLL for a program, and want to be able to run that program in a command line, and then pipe the output of my DLL to another program. How do I do this?
I can currently opening a new debug console to print to, but I want to be able to pipe the output so that I can run a shortcut like:
C:\Windows\System32\cmd.exe /K "C:\Program_Using_the_DLL.exe | C:\Program_to_Pipe_To.exe"
This is different from some similar questions (IE: this one) in that I need to be able to pipe the output, so I need it to come to the current console.
Perhaps I can use GetStdHandle() somehow, or is there someway of using AttachConsole(ATTACH_PARENT_PROCESS)? Any help would be appreciated! I'm new to this nitty-gritty part.
(DLL is written in C++)
Try using the function GetStdHandle(STD_OUTPUT_HANDLE) to get a HANDLE to the current output file.

Why would a program run when launch from windows but not the command prompt?

I wrote a small C++ program in VS2k8. When I launch it from windows (double click the exe file) it runs fine. When I go to the command prompt and try to run it it will hang and eventually crash. I've created test programs with simple outputs that work fine both ways.
Is there something I'm missing? I'm relatively new to programming. I'm trying to launch this program using the VBA shell command but it produces the same outcome as the command prompt.
The funny thing is it was working fine at first until I went in to change the value of a constant variable and rebuilt it (I didn't think that had anything to do with it but I changed it back with no success). No settings where changed.
Edit: I've name it time.exe and than copies.exe (when I tried copying and pasting the code into a new project). The actual code is about 250 lines, not sure what part of it would be causing the issue. It opens a .csv file, loads the information into vectors, and then compares the vectors to each other (adding something to the end of it if it meets certain conditions). It than outputs the file to another .csv file.
Might suggest that the current directory on start up is different and this is causing your issue as you make some assumptions about the current path or drive?