Thread: concatenation of strings and integers then call into a function

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    12

    concatenation of strings and integers then call into a function

    I have a very simple question. I would like to concatenate strings so that
    Code:
    char *string0 = "/home/myComputer/someFolder/fileName_";
    char ext[4] = ".raw";
    char fname[256];
    
    int someNumber;
    
    sprinrf(fname, "%s%d%s", string0, someNumber, ext); // my problem is here
    
    int begin = 0;
    int end = 10;
    
    someFunction(fname, begin, end);
    What i want to do is if I call someFunction (....), I want someNumber to be free so that i can use the variables begin and end to replace it , so that
    someFunction("/home/myComputer/someFolder/fileName_%d.raw", begin, end);


    Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    And what exactly is the problem?

    You may want to assign a value to your number before you try to use it. And your ext[] doesn't have enough room for the required end of string character. You need at least a size of 5.

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    12
    Thanks Jim.
    Thanks for pointing out the mistake in ext[].

    someFunction is part of a bigger for-loop so that when someFunction is called, it will do some more complicated for-loops. The idea is, I want define a larger string first by concatenation of a string, an integer and another string. However the integer will only be supplied only when the string is used in someFunction(....)

    There might be a better way to address this???

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Assuming you fixed the problem that jimblumberg mentioned with ext, and assuming you spell sprintf correctly, it seems it should work for concatenating the number as you described. What is the problem you are having?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The idea is, I want define a larger string first by concatenation of a string, an integer and another string. However the integer will only be supplied only when the string is used in someFunction(....)
    Then you need to wait to concatenate the strings until you know the number.

    Jim

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >The idea is, I want define a larger string first by concatenation of a string, an integer and another string. However the integer will only be supplied only when the string is used in someFunction(....)
    Your for certain haven't explained the problem well enough to get an answer. I've read it several times your post and still couldn't construct the problem you immesly facing.

    Your trying to construct larger string by concatenating several other sub string. Amongs the sub string one of them is a integer. But the integer value has no value until is gibe through a paramter to the function when its called. And you still haven;'t explained the begin and the end variables usages. Are there any impack on the integer value your trying to assign?

    You can always have a control statement to control the concatenation, to make sure you have the integer values assigned with a value.
    Code:
    if( someInteger != 0 )
    {
        concatenate the sub string;
    } else { skip and continue }
    It could be as simple as that. If understand you well. And initialise the local integer varibale.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by fclopez View Post
    What i want to do is if I call someFunction (....), I want someNumber to be free so that i can use the variables begin and end to replace it , so that
    someFunction("/home/myComputer/someFolder/fileName_%d.raw", begin, end);
    You have to first create the format string by concatenating all the parts (including the %d format specifier) and then later use this format string:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *string0 = "/home/myComputer/someFolder/fileName_";
        char ext[4] = ".raw";
        char fname[256];
         
        puts("Create format string for later processing:");
        sprintf(fname, "%s%s%s", string0, "%d", ext); 
        puts(fname);
         
        int begin = 0, end = 10, i;
        
        puts("\nNow insert number into created format string:");
        for (i = begin; i < end; i++)
        {
            printf(fname, i);
            putchar('\n');
        }
            
        return 0;
    }
    Is that what you want?

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to convert integers into strings.
    By inobProgram in forum C++ Programming
    Replies: 5
    Last Post: 07-17-2012, 01:59 PM
  2. Strings as integers
    By unknown_ in forum C Programming
    Replies: 3
    Last Post: 03-19-2010, 09:40 PM
  3. concatenation of strings
    By cnu_sree in forum C Programming
    Replies: 2
    Last Post: 02-12-2008, 11:23 PM
  4. Concatenation of strings as char pointers
    By sameerc in forum C Programming
    Replies: 11
    Last Post: 05-10-2005, 04:23 PM
  5. integers in reverse with a function call
    By dwilson in forum C Programming
    Replies: 3
    Last Post: 04-06-2003, 08:50 AM