Thread: sprintf() question

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    22

    sprintf() question

    I am using readline() to get input from a console. I am saving the input strings into a char variable. Then i sprintf the char varible into a buffer by using sprintf(). How can i divide the buffer into a number of arguments?

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    22
    so for example i typed "Hello World" .. how can I divide the buffer to say ..argv[0] is Hello and argv[1] is World ....or divide it by number of argc

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, you can use sscanf() to scan words from a string, e.g.
    Code:
    char buffer[BUFSIZ], word1[BUFSIZ], word2[BUFSIZ];
    strcpy(buffer, "Hello World");
    sscanf(buffer, "%s%s", word1, word2);
    Or use strtok(). This will split up data that you have in some array from readline(). I'm not sure why you're mentioning argv[0] and argv[1] unless it's as an example, because argv[] is already split up into words. Can you give more details?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-12-2010, 08:02 PM
  2. Quick question on sprintf and memcpy
    By vandrea in forum C Programming
    Replies: 5
    Last Post: 08-23-2009, 12:53 PM
  3. sprintf()
    By BMathis in forum C Programming
    Replies: 2
    Last Post: 02-16-2009, 06:13 PM
  4. question about sprintf()
    By Bleech in forum C Programming
    Replies: 4
    Last Post: 07-27-2006, 12:37 PM
  5. sprintf question
    By aaronc in forum C Programming
    Replies: 2
    Last Post: 05-25-2004, 11:11 PM