#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0

    C++ system() function


    Hi guys Im a newbie when it comes to C/C++,

    I have been trying to make a system() call to DIR in windows, and have the dir switches with the call. but I cant combined the commands to do so.. help! :)
    Code:
    #include "stdafx.h"
    #include "stdlib.h"
    
    int main(int argc, char* argv[])
    {
    char* string;
    char* command;
    command = "dir " + argv[1];
    // I have tried everything I can find or thank up, but I cant 
    // combined "dir" and argv[1] into one string like so "dir argv[1]"
      puts ("Executing command...");
      system(command);
    return 0;
    }
    Last edited by z3d; September 21st, 2003 at 05:12 PM.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    First, why are you making a system call within your code? There are all the functions of the OS available via C calls, just do some research. Second, you are allocating no memory for your character array variables, just pointers. Third, you can't concatenate characters arrays with a plus, you need to use strcat or sprintf. If you want that sort of functionality, read up on std::string or MS's CString.

    I presume you are trying to do what works in Perl or some other programming language? C is very basic and you have to do most of the work yourself (which is why people invented all those other languages!).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0
    True people have invented other languages! But this is what im learning(C/C++), and this is what im doing, so this is what i need to know! EOF.

    My new code:

    Code:
    int main(int argc, char* argv[])
    {
    char str[256];
    strcpy (str,"dir ");
    strcat (str, argv[1]);
    	
    	puts ("Execute command...");
    	system(str);
    return 0;
    }
    This does compile but it will crash, with errors when ran.
    To clear things up, im running on a xp box, and building with VC++. 2nd I know I can go download it, and I could write allot more code to make its stand on its own. and I could write a batch script to do this. But Im NOT!

    so..... If I have not done it! It is because I dont know about it! Im new at C++.
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    I ran your exact code on Win2K with VC++ 6 and it ran fine. Are you supplying an argv[1]? I got a crash when I failed to do so.

    I have some example code here http://sol-biotech.com/code/WinDirectoryListing.cpp that you may find useful in getting directory listings in Windows.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0
    Your right it does work! lol how odd... Was not running the last time i tested it.

    Well thank, and thanks for the link.

    final code:
    Code:
    // ls.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "stdlib.h"
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char* argv[])
    {
    
    if(argc!=2)
      {
    
    	puts ("Execute command...");
    	system("dir");
    	return 0;
    }
    
    char str[256];
    strcpy (str,"dir ");
    strcat (str, argv[1]);
    	
    	puts ("Execute command...");
    	system(str);
    return 0;
    }
    Could you tell me how to build a if statement for something like /help or -help?

    I tried this:

    if(argc!="/help")
    {
    // content.
    }
    Last edited by z3d; September 22nd, 2003 at 12:10 AM.
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    In C, strings are really arrays of char. So you can't do something like this:
    if(argc!="/help")
    {
    // content.
    }

    Instead, what you need to do is compare two arrays. There's already a prewritten function to do this for char arrays, called strcmp() -- this function compares two strings and returns a value as follows:
    1. Number less than zero -- string1 is less than string2
    2. Zero - string1 is identical to string2
    3. Number greater than zero - string1 is greater than string2.

    BTW, you should be comparing an element from the argv[] array, not argc. argc is an int value and represents the number of elements in the argv[] array (i.e.) argc is the number of arguments that the program received on the command line.
    Code:
    if (strcmp(argv[1], "/help") == 0) {
       //content
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo