Help with handling strings

This is a discussion on Help with handling strings within the C Programming forums, part of the General Programming Boards category; I'm writing a program that will send mail through an smtp server. I can send the commands fine by themselves ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    14

    Help with handling strings

    I'm writing a program that will send mail through an smtp server. I can send the commands fine by themselves but when I try to send them with user input (like asking for the desintation addr instead of hard coding it) I have problems.

    I want the output to look like
    MAIL From:<mr54561@swt.edu>
    RCPT TO:<mr54561@swt.edu>

    Here is my output and below is the relevant code, if anyone could help it would be much appreciated. Thanks

    Output

    Code:
    [mr54561@taz3 networking]$ ./usermail mr54561@swt.edu
    (From) :lkj@lkj.com
    RCPT To:<mr54561@swt.edulkj@lkj.com>
    
    Next is..
    @swt.edulkj@lkj.com>

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <strings.h>
    
    #define SIZE sizeof (struct sockaddr_in)
    
    main ( int argc, char *argv[]  ) 
    {
      int sockfd, i;
    
    char *fromer;
    char helo[] = "HELO \n";
    char kara[] = "MAIL From:<\n";
    char to[] = "RCPT To:<";
    char return_val[1024];
    char input[1024];
       char c, rc;
       struct sockaddr_in server;
    
    if (argc!=2 || argc>2){
    printf("Correct syntax is 'usermail <recipient>'\n");
    exit(1);
    }
    
    strcat(to,argv[1]);
    strcat(to,">\n");
    
    
    
    printf("(From) :");
    gets(fromer);
    
    strcat(kara,fromer);
    strcat(kara,">\n");
    
    printf("%s\n",to);
    printf("Next is..\n");
    printf("%s\n",kara);
    Last edited by LinMach; 04-18-2003 at 03:47 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Salem
    char *fromer;
    gets(fromer);
    Both these are wrong - fromer isn't pointing to any memory (or rather, a random area of memory), and you're using gets()
    See the FAQ on why this is dangerous, and how to fix it.
    Awww, you mean gets would be a bad thing in a CGI or mailto form?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    Originally posted by Salem
    char to[] = "RCPT To:<";
    ...
    strcat(to,argv[1]);
    strcat(to,">\n");

    C only allocates enough space in your array for the initial string - if you want to append data to it later on, then you must allocate space accordingly

    char to[100] = "RCPT To:<";
    ....
    strcat(to,argv[1]);
    strcat(to,">\n");
    Salem, you just made my day a whole lot brighter. Thank you very much.


    Mike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-29-2008, 05:29 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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