Thread: Variable strings

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Variable strings

    I have an array of strings char string[n][MAX_FILE_NAME] and in each string I want to initialize it with the name of a bunch of files I have. string[0] should be "txt_0", string[1] = "txt_1" etc.

    But I don't know how to construct the string names beginning with txt_ and ended in the int counter value.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Use a loop containing something like
    Code:
       sprintf(string[i], "txt_%d", i);
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You probably can use snprintf. Something like this might work

    Code:
    snprintf(str, MAX_FILE_NAME, "txt_%d", 1);

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Ahh nice, an easy solution. Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-16-2011, 12:28 PM
  2. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  3. Variable length strings
    By MTK in forum C Programming
    Replies: 9
    Last Post: 08-30-2009, 06:23 PM
  4. append 2 strings into one variable
    By stormy in forum C Programming
    Replies: 3
    Last Post: 08-15-2005, 01:39 AM
  5. variable length records for strings
    By teja22 in forum C Programming
    Replies: 1
    Last Post: 02-08-2002, 07:49 PM