Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello all! I have a question on how to run a shell command in C and see the results. So far this is the code i have. When i compile and run it i just get nothing.
#include <stdio.h>
#include <stdlib.h>int system(const char *string1);
int main()
{
char string1 = "ifconfig";
system(string1);
return(0);
}Any help with be great. Im not wanting to use ifconfig but i just threw it in there as an example.
Thanks in advance.C

You are probably not getting values because ifconfig may not be a command (it isn't on my local box)... You are probably wanting to call 'ipconfig'.
There seem to be a few errors in your code that may or may not let it compile...
Notice:
1) No need to declare a system function, it should be included...
2) char string1 = "something"; will not work. You need to use *string1 as my example shows below.
3) Pause before and after so you can see if you are getting any results back, even a command error...
#include <stdio.h>
#include <stdlib.h>int main (void)
{
char *string1 = "ipconfig";
system("pause");
system(string1);
system("pause");
return(0);
}IR

ipconfig is a DOS command. If you noticed in the OS on the top im on Linux. ifconfig is the same as ipconfig in the DOS world. In linux im launching it in a shell so no need for the pause.
Try again

Ok. I missed your OS... not that it really matters in this case. However, just to pacify the question, I compiled and executed the code below and it works perfectly.
#include <iostream>
int main (void)
{
char *cmd = "ls -al";
system(cmd);
return 0;
}

hello seeJ,
i am not so sure why you need the code
int system(const char *string1);
in your program, ie. the system function declaration, system() is in stdlib.h; think that is the problem.
=)

Hi, I haven't got a compiler with me
but this routine could do the job quickly#define MAX_STRING 256
char *IntToBinary (int number)
{
static char binaryStr[MAX_STRING];
int i;/* Reset string with 0s to set string terminator */
memset(&binaryStr, '\0', sizeof(binaryStr));for (i = sizeof(int); i > 0; i--)
{
binaryStr[i] = (number & 1) + '0';
number = number >> 1;
}
return binaryStr;
}

![]() |
Declaring global variable...
|
calling batch file btween...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |