I am creating a windows application that must capture results of several DOS Commands without the command prompt opening and save them into a string. I am using the borland libraries.
system("dir") therefore is not good. The result of each command needs to be written to a string variable so I can write it to a log and a separate file.
I have an XML file where they are defined.
The thing i'm struggling on the most is actually capturing the output into a string variable. I have heard of _popen but having problems trying to use it.
I think what you really want is freopen() that allows you to redirect the usual streams to a file (either temporary or the separate file you mentioned), and when the process closes, read the file into your log.
I assume the 'result' is the output from the command, and not its return value.
Related
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
I have a simple program that reads in a basic config file which contains a user provided path. I'm trying to use that path to call system to execute a .bat that resides at the location provided. I'm able to pass the stored path directly into system and it attempts to run the .bat fine, but it needs to try and run it as an admin. I came across the following post: How to call system() in an opened administrator program and gives it the same privileges?
I'm building the string as indicated in the above post, but when I try and pass this new string into system, it tells me "The system cannot find the file specified". Here's the (most likely wrong) way I'm building the string that I pass into system.
std::string adminFilePath = "runas /user:<admin-user> \"";
adminFilePath.append(configFileSettings.path.c_str()); //Append the path of the file that we got from the config file.
adminFilePath.append("\"");
system(adminFilePath.c_str());
My assumption is that I'm should be trying to build a basic string representing what I'd type right into a cmd window to execute the .bat, but obviously I'm wrong somewhere.
Check file name, that you dont use single . Check current folder for program you'really running if path is relative Do not use system... especially if you have non-an so folder/file names. system () is ancient attempt to implement posix function and supports only ANSI and may be confused by modern quoted arguments as well. Use execve or spawn.
In fact, you can avoid running runas at all Requesting administrator privileges at run time
I am not sure how to explain this, so I will give a scenario.
I want to write a program, which will be set as the default program for a file extension. When the program opens, it will output everything in the program. i.e file.txt contains the word hello, and when opened, my program opens and displays the word hello.
The idea is like Notepad. When a text file is opened, the contents are displayed. However, mine will be in a DOS window.
How can I achieve this?
Sorry if there is another question like this somewhere on here, TBH I am not too sure what this is called and thus can't search.
EDIT: My apologies. I am running Windows 7
If you open a file "with" your program (for instance using file type associations or by dragging the file onto the .exe), then the filename of the data file is passed as the command-line parameter to your application.
See:
What are the arguments to main() for?
I want to write a program, which will be set as the default program for a file extension.
See:
How File Associations Work
File Types and File Associations
You will associate your app with file extension and your app will do whatever it has to do.
I want to use an .exe file inside my c++ program. I reviewed and checked these functions (system - ShellExecute - CreateProcess) to achieve this goal, but I found them useless because I need the output of that .exe file inside my program for further processes.
CreateProcess can be used to pipe the output from the created program back to the creator. MSDN even has sample code to do exactly this: Creating a Child Process with Redirected Input and Output
I think you do want CreatProcess. You can get the STDIN / STDOUT:
http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx
you should use the "Process.standardOutput" to read the result of an .exe.
Here is the link on MSDN, there is plenty of explainations:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.71).aspx
You could use one of those functions to execute your file and have it write the output to another file. Then you just have to read that into your program.
I have a c++ program (very complicated, and lengthy both in code and execution time).
Once in a while this program stops and calls a user-specified shell script.
Before calling the script, my program creates a .out file with current data. I call the script via system() command. The script then reads the .out file, and creates its own script.out file and exits.
Then the system() function call ends, and my program reads and parses the script.out file.
Question: is there a better way to execute communication between my c++ program and a random shell script?
My intent is to have full communication between the two. Script could virtually "ask" the program "What data do you have right now?" and the program would reply with some strict convention. Then the script could say "Add this data...", or "delete all your previous data" etc.etc.
The reason I need this is because the shell script tells the program to modify its data. The exact data that was put in the original .out file. So after the modification is done -- the actual data held by the program does not correspond to the data written in the .out file.
Thanks!
P.S.
I swear I've searched around, but everyone suggests an intermediate file.
There are certainly ways to do that without intermediate files. The most common approach is to use command line arguments for input, and pipes for standard output; others also use pipes for input. The most straight-forward alternative to system then is to use popen.
On a unix-like system? Perhaps pipe (2) will work for you?
From the man page (Mac OS X 10.5 version):
SYNOPSIS
#include <unistd.h>
int pipe(int fildes[2]);
DESCRIPTION
The pipe() function creates a pipe (an object that allows unidirectional
data flow) and allocates a pair of file descriptors. The first descrip-
tor connects to the read end of the pipe; the second connects to the
write end.
You will, of course, have to follow the creation of the pipes with a fork and exec pair. Probably this has already been answered in detail, and now you know what to search on...
It's been a while since I did this, but:
In the main process, before forking the sub-process you call pipe twice. Now you have two pipes and control both ends of both of them.
You fork.
The main process will read from one pipe and write from the other. It doesn't matter which is which, but you need to be clear about this.
The child process will call one of the exec family of function to replace it's image with that of the shell you want to run but first you will use dup2 to replace it's standard input and output with the ends of the two pipes (again, this is where you need to be clear about which pipe is which).
At his point you have two processes, the main process can send things into one pipe ad they will be received on the standard input of the script, and anything the script writes to it's standard output will be sent up the other pipe to the controlling process. So they take turns, just like interacting with the shell.
You can use pipes or (maybe more convenient) sockets - for example frontends to gdb, or expect do that. It would require changes to your shell scripts, and switching from system() to more low-level fork() and exec().
It's rather complicated so please, be more specific about your environment and what you need to clarify.
You are asking the question on Interprocess Communication (IPC).
There are a lot of ways to do that. You can do a simply search and Internet will return you most answers.
If I am not wrong, Google chrome uses a technique called Named Pipe.
Anyway, I think the most "portable way" is probably a file. But if you know you are working on which operating system, you can definitely use most of the IPC techniques.