Jump to content

Hiding CMD window using C++

Shammikit
Go to solution Solved by WereCatf,
14 minutes ago, Shammikit said:

hello i have got Qprocess working and in my case im getting 2 lines as output.If im accessing the second output only, how can i achieve this. is there a way to remove the first line of output or is there a way to tell to read the 2nd line only?

It'd help, if you showed the output you're getting. That said, there are multiple ways of skinning the cat, like e.g. using strchr() or strtok(). Here's a quick example of how to do it using strchr():
 

#include <stdio.h>
#include <string.h>

int main ()
{
  char multiString[] ="First line of text.\nSecond line of text.";
  char *singleString = NULL; //Always initialize pointers!
  singleString = strchr(multiString, '\n'); //Look for newline-character
  if(singleString != NULL) singleString++; //Skip the newline-character
  if(strlen(singleString)) printf("Output: %s\n", singleString);
  return 0;
}

This example will produce:

Quote

Output: Second line of text.

 

im writing a QT window application and im using popen to use CMD to run a command, get its output and display it. when running it open the cmd window for like a second  and goes out. the code works and i get my output in the label . I do not like seeing that cmd window pop up so i want to know if there's a method to hide the window when its running. Thank you

#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <string>

std::string exec(const char* cmd) {
    char buffer[128];
    std::string result = "";
    FILE* pipe = popen(cmd, "r");
    if (!pipe) throw std::runtime_error("popen() failed!");
    try {
        while (!feof(pipe)) {
            if (fgets(buffer, 128, pipe) != NULL)
                result += buffer;
        }
    } catch (...) {
        pclose(pipe);
        throw;
    }
    pclose(pipe);
    return result;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

I believe you should use QProcess instead, it apparently shouldn't open up a window. Doesn't look like there's a way of stopping that from happening when using popen().

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

28 minutes ago, WereCatf said:

I believe you should use QProcess instead, it apparently shouldn't open up a window. Doesn't look like there's a way of stopping that from happening when using popen().

hello i have got Qprocess working and in my case im getting 2 lines as output.If im accessing the second output only, how can i achieve this. is there a way to remove the first line of output or is there a way to tell to read the 2nd line only?

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Shammikit said:

hello i have got Qprocess working and in my case im getting 2 lines as output.If im accessing the second output only, how can i achieve this. is there a way to remove the first line of output or is there a way to tell to read the 2nd line only?

It'd help, if you showed the output you're getting. That said, there are multiple ways of skinning the cat, like e.g. using strchr() or strtok(). Here's a quick example of how to do it using strchr():
 

#include <stdio.h>
#include <string.h>

int main ()
{
  char multiString[] ="First line of text.\nSecond line of text.";
  char *singleString = NULL; //Always initialize pointers!
  singleString = strchr(multiString, '\n'); //Look for newline-character
  if(singleString != NULL) singleString++; //Skip the newline-character
  if(strlen(singleString)) printf("Output: %s\n", singleString);
  return 0;
}

This example will produce:

Quote

Output: Second line of text.

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, WereCatf said:

It'd help, if you showed the output you're getting. That said, there are multiple ways of skinning the cat, like e.g. using strchr() or strtok(). Here's a quick example of how to do it using strchr():
 


#include <stdio.h>
#include <string.h>

int main ()
{
  char multiString[] ="First line of text.\nSecond line of text.";
  char *singleString = NULL; //Always initialize pointers!
  singleString = strchr(multiString, '\n'); //Look for newline-character
  if(singleString != NULL) singleString++; //Skip the newline-character
  if(strlen(singleString)) printf("Output: %s\n", singleString);
  return 0;
}

This example will produce:

 

Sorry for the late response. I was messing around with this code until i got it running the way i want it to be. Thank you for ur great help.:)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×