Passing variables to system() function?

This is a discussion on Passing variables to system() function? within the C Programming forums, part of the General Programming Boards category; Hi there everybody. I'd like to be able to supply my program with a drive letter, and have the program ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Passing variables to system() function?

    Hi there everybody.

    I'd like to be able to supply my program with a drive letter, and have the program use the system() function on this target drive (like a batch file). The below is an example of what I've tried, but the system() line errors.

    Code:
      char drive[1];
      printf("Target drive is: ");
      scanf("%s", drive);
      printf("\nOkay, using %s: as target drive...\n", drive);
      system("dir %s:", drive);
      system("PAUSE");
    Sorry for the stupid question!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    > char drive[1];
    This isn't large enough to hold a string input from scanf %s

    > system("dir %s:", drive);
    Use sprintf() to construct the command line, then use the result of that with system()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Use sprintf before calling system.
    Code:
      char input[10], drive[10];
      char command[100];
    
      printf("Target drive is: ");
      fgets(input, 10, stdin);
      if (sscanf(input, "%s", drive) != 1)
      {
        printf("Error entering target drive.");
        return EXIT_FAILURE;
      }
      printf("\nOkay, using %s: as target drive...\n", drive);
      sprintf(command, "dir %s:", drive);
      system(command);
      system("PAUSE");

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Thank you kindly guys, I get it now.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    One more follow up question about system(); guys....

    when I do a

    Code:
    copy C:\directory\file.txt I:\directory
    from the command line it just goes without an problems. However if I try the same thing in this program by going

    Code:
    sprintf(command, "copy C:\directory\file.txt %s:\directory", drive);
    system(command);
    it thinks for a second and then just says "0 files copied". What gives? It should work as if it was from the command line, right? Thanks!

    (I know there's a much easier way to copy files in C, this is just an example!)

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Try forward slashes in string literals. Otherwise escape backslashes as \\.

    [edit]printf("command = \"%s\"\n", command); /* debugging */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. passing variables from main to another function
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-06-2006, 07:30 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Passing system time to a function
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21