Thread: Help in using variable in system command

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    13

    Help in using variable in system command

    Hey, I'm trying to make a simple net send application with no GUI. The program basically asks for the hostname, the message, then proceeds with filling out the information which is needed for the system command net, command option send. (net send host message).

    I have this so far, but it just types out the whole string into the system command, not including the values within the variables. I am quite new to C so I don't have much of an idea of what I should be doing. Here's how it looks:

    Code:
    #include <stdio.h>
    #include <dos.h>
    
    int main()
    {
    char host, msg;
    
    printf("\nEnter the hostname: ");
    scanf("%d", &host);
    printf("\nEnter your message: ");
    scanf("%d", &msg);
    system("net send %d %d", &host, &msg);
    system("pause");
    }

    Tell me what you think, any help is greatly appretiated.

    Gordon.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <stdio.h>
    //#include <dos.h>  // Not needed
    #include <stdlib.h>  // Needed for system command
    
    int main()
    {
        // A simple char is not enough to store a string... you need
        // to use an array of characters
        //char host, msg;
        char host[20], msg[20];  // 20 is just an example
        char command[60];  // This is for later
    
        printf("\nEnter the hostname: ");
        // You need to use the proper format specifier for the scanf function call
        // %d is for reading in integers, %s is for reading in strings (arrays of
        // characters).  Also, in this instance you don't need the address-of operator (&)
        // In front of the variable name
        //scanf("%d", &host);
        scanf("%s", host);
        printf("\nEnter your message: ");
        scanf("%s", msg);
    
        // Now you need to use the sprintf function to format the string you wish to send
        // to the system function.
        sprintf(command,"net send %s %s", host, msg );
    
        // The system function only accepts a single argument
        //system("net send %d %d", &host, &msg);
        system( command );
        system("pause");
    
        return 0;
    }
    Last edited by hk_mp5kpdw; 08-25-2005 at 10:04 AM. Reason: Messed up one of the headers
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    Wow cool that makes a lot of sense, thanks a heap for you're time typing all the comments up, really helps understand what I'm doing wrong. I totally missed the %d and %s

    I always thought I needed dos.h for system commands, for some reason.

    Again, thanks a lot for your help.
    Last edited by moagim; 08-25-2005 at 10:02 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    13

    Problem

    Hmm Ok, I've got another slight problem when I pass more than one parameter to msg, here's what the code looks like:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    int count;
    char host[20], msg[80];
    char command[60];
    
    printf("Enter the amount of messages you want to send: ");
    scanf("%d", &count);
    
    while (count > 0) {
    printf("\nEnter the hostname: ");
    scanf("%s", host);
    printf("\nEnter your message: ");
    scanf("%s", msg);
    printf("\n\n");
    sprintf(command,"net send %s %s", host, msg );
    system( command );
    count--;
    }
    system("pause");
    return 0;
    }
    Heres the error I get from command prompt:

    Enter the amount of messages you want to send: 2

    Enter the hostname: pc1

    Enter your message: This is a message


    An error occurred while sending a message to PC1.

    The message alias could not be found on the network.

    More help is available by typing NET HELPMSG 2273.


    Enter the hostname:
    Enter your message:

    An error occurred while sending a message to IS.

    The message alias could not be found on the network.

    More help is available by typing NET HELPMSG 2273.

    Press any key to continue . . .

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  6. #6
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    That's not an error from your program, it's an error from net

    net can't find the hostname you used, it's either not aliased on your system to an IP
    Or unknown to the DNS server.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    Quote Originally Posted by Sysop_fb
    That's not an error from your program, it's an error from net

    net can't find the hostname you used, it's either not aliased on your system to an IP
    Or unknown to the DNS server.
    No I know thats an error from net, but if you take a look at the second net command, it is trying to use the second param i passed in msg as the hostname. Thats my point, it only lets me send a message with one parameter, if two are included then that second word gets sent into "host" variable, making the program kinda useless.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This has to do with the newline left over in the input buffer satisfying the subsequent scanf call without waiting for your further input. Obtain your string input differently, perhaps taking my suggestion and following the link I posted previously. Also, avoid scanf.
    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.*

  9. #9
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    I rewrote it using fgets on my system and it works fine for me.
    Scanf just causes my system to act funky... ofcourse it is fairly old... QNX4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. System command
    By hello in forum C Programming
    Replies: 3
    Last Post: 01-09-2009, 05:28 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Replies: 1
    Last Post: 03-11-2003, 05:36 PM
  5. Replies: 2
    Last Post: 02-28-2002, 03:27 PM