0

I am very fluent when it comes to batch scripting but my c++ knowledge is not so strong. I would like to know how to create a c++ program that:

  1. contains an input field that the user can input text
  2. the c++ program calls the batch file and feeds information into the batch file
  3. the batch file works on this input, then feeds the output back into the c++ program
  4. the c++ program displays this output on screen.

please not that this is strictly between BATCH and C++ and is NOT duplicate of a previously asked question. thanks in advance.

Nir Friedman
  • 17,108
  • 2
  • 44
  • 72
Jahwi
  • 426
  • 3
  • 14

1 Answers1

1

In order to have a text field, you need a GUI. GUI is not an inherent part of C++, so you have to use some of the GUI frameworks like Qt. Theoretically, you can do it with WinAPI only, but it would be painful I suppose. In my sample I take input number from console.

Regarding calling batch and passing arguments, it is simple. Just call process cmd.exe /C {yourbatch}.bat {parameters}. Note that you can pass input data to batch as command line parameters (also, you can use environment variables). In order to start exe-file, you can use CreateProcess function of WinAPI (I have taken sample from this answer)

Getting output is a bit harder. One of the ways is to capture console output stream of your batch with a pipe. Something like this... However, it is much easier to simply use files to pass any data.

Sample C++:

#include <stdio.h>
#include <windows.h>

int main() {
    int n;
    scanf("%d", &n);                            //read integer n from console

    int *arr = new int[n];                      //create array of 1..n integers
    for (int i = 0; i < n; i++)
        arr[i] = i + 1;

    char cmd[1024] = "cmd.exe /C script.bat";   //base command line
    for (int i = 0; i < n; i++)                 //add array elements as console parameters
        sprintf(cmd, "%s %d", cmd, i);

    STARTUPINFO info = {sizeof(info)};          //create cmd.exe process
    PROCESS_INFORMATION processInfo;
    if (CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) {
        WaitForSingleObject(processInfo.hProcess, INFINITE);
        CloseHandle(processInfo.hProcess);      //note that we wait for completition
        CloseHandle(processInfo.hThread);
    }

    int sum;
    FILE *f = fopen("sum.txt", "r");            //read sum from 'sum.txt'
    fscanf(f, "%d", &sum);
    fclose(f);

    printf("Sum of 1..%d is %d\n", n, sum);     //print the sum
    return 0;
}

Sample Batch (must be named script.bat in working directory):

del sum.txt
set sum=0
for %%x in (%*) do (                            //sum all the parameters
    set /A sum+=%%~x
)
echo %sum% >sum.txt                             //write result to sum.txt
Community
  • 1
  • 1
stgatilov
  • 5,333
  • 31
  • 54
  • sorry but that is C code, not C++ – AndersK Jul 28 '15 at 05:26
  • @CyberSpock It perfectly works in C++. Replacing `int*` with `std::vector` does not make this piece of **** any better =) – stgatilov Jul 28 '15 at 05:27
  • it is still C code IOW: a C++ programmer wouldn't write it like that, a C programmer would. – AndersK Jul 28 '15 at 05:28
  • Mixing WinAPI with STL seems weird to me. Perhaps there is process creating routine in boost, but **do you really want to ask a C++ novice to use boost?** – stgatilov Jul 28 '15 at 05:35
  • it works perfectly well to use STL with WinApi e.g. 'std::vector cmd(1024); .. Createprocess(nullptr, &cmd[0],... )' not sure why you mention boost? iostream gives type safety and convenienc etc. – AndersK Jul 28 '15 at 05:57