Inserting “String” variable inside a given String in C









up vote
0
down vote

favorite












I have a function, which has a String Parameter:



function(char str[3])
//here i want to insert the string Parameter str
f = open("/d1/d2/d3/test"+str+"/d2.xyz")



I am trying to "insert" the String parameter into the given String path. How can I do this in C?










share|improve this question

















  • 1




    Research snprintf(buf, sizeof buf, "/d1/d2/d3/test%s/d2.xyz", str).
    – chux
    Nov 10 at 20:55











  • Note that although the argument definition (char str[3]) suggests that the string is limited to 2 characters plus null, this is not enforced. The parameter is treated as if you'd written char *str and any length could be provided — including zero bytes of string.
    – Jonathan Leffler
    Nov 10 at 21:10










  • why is it limited to 2 chars? isnt it a char array like: [0,1,2,3] ?
    – simplesystems
    Nov 11 at 7:11














up vote
0
down vote

favorite












I have a function, which has a String Parameter:



function(char str[3])
//here i want to insert the string Parameter str
f = open("/d1/d2/d3/test"+str+"/d2.xyz")



I am trying to "insert" the String parameter into the given String path. How can I do this in C?










share|improve this question

















  • 1




    Research snprintf(buf, sizeof buf, "/d1/d2/d3/test%s/d2.xyz", str).
    – chux
    Nov 10 at 20:55











  • Note that although the argument definition (char str[3]) suggests that the string is limited to 2 characters plus null, this is not enforced. The parameter is treated as if you'd written char *str and any length could be provided — including zero bytes of string.
    – Jonathan Leffler
    Nov 10 at 21:10










  • why is it limited to 2 chars? isnt it a char array like: [0,1,2,3] ?
    – simplesystems
    Nov 11 at 7:11












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a function, which has a String Parameter:



function(char str[3])
//here i want to insert the string Parameter str
f = open("/d1/d2/d3/test"+str+"/d2.xyz")



I am trying to "insert" the String parameter into the given String path. How can I do this in C?










share|improve this question













I have a function, which has a String Parameter:



function(char str[3])
//here i want to insert the string Parameter str
f = open("/d1/d2/d3/test"+str+"/d2.xyz")



I am trying to "insert" the String parameter into the given String path. How can I do this in C?







c string path






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 20:52









simplesystems

3001317




3001317







  • 1




    Research snprintf(buf, sizeof buf, "/d1/d2/d3/test%s/d2.xyz", str).
    – chux
    Nov 10 at 20:55











  • Note that although the argument definition (char str[3]) suggests that the string is limited to 2 characters plus null, this is not enforced. The parameter is treated as if you'd written char *str and any length could be provided — including zero bytes of string.
    – Jonathan Leffler
    Nov 10 at 21:10










  • why is it limited to 2 chars? isnt it a char array like: [0,1,2,3] ?
    – simplesystems
    Nov 11 at 7:11












  • 1




    Research snprintf(buf, sizeof buf, "/d1/d2/d3/test%s/d2.xyz", str).
    – chux
    Nov 10 at 20:55











  • Note that although the argument definition (char str[3]) suggests that the string is limited to 2 characters plus null, this is not enforced. The parameter is treated as if you'd written char *str and any length could be provided — including zero bytes of string.
    – Jonathan Leffler
    Nov 10 at 21:10










  • why is it limited to 2 chars? isnt it a char array like: [0,1,2,3] ?
    – simplesystems
    Nov 11 at 7:11







1




1




Research snprintf(buf, sizeof buf, "/d1/d2/d3/test%s/d2.xyz", str).
– chux
Nov 10 at 20:55





Research snprintf(buf, sizeof buf, "/d1/d2/d3/test%s/d2.xyz", str).
– chux
Nov 10 at 20:55













Note that although the argument definition (char str[3]) suggests that the string is limited to 2 characters plus null, this is not enforced. The parameter is treated as if you'd written char *str and any length could be provided — including zero bytes of string.
– Jonathan Leffler
Nov 10 at 21:10




Note that although the argument definition (char str[3]) suggests that the string is limited to 2 characters plus null, this is not enforced. The parameter is treated as if you'd written char *str and any length could be provided — including zero bytes of string.
– Jonathan Leffler
Nov 10 at 21:10












why is it limited to 2 chars? isnt it a char array like: [0,1,2,3] ?
– simplesystems
Nov 11 at 7:11




why is it limited to 2 chars? isnt it a char array like: [0,1,2,3] ?
– simplesystems
Nov 11 at 7:11












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The typical way would be to create a new string by piecing together the three pieces. One way do to this would be the following (shamelessly stolen from the @chux comment):



char buf[1000];
sprintf(buf, “/d1/d2/d3/test%s/d2.xyz”, str);


But before you go that route you need to ensure you really understand the printf family of functions as they are a common source of security related errors. For example, my buf size is large enough for your example, but certainly not for a general solution. Instead the sizes of the input strings would need to be taken into account to ensure the output buffer is large enough.






share|improve this answer




















  • This would also fail if str is not properly null-terminated.
    – Steven W. Klassen
    Nov 10 at 21:09










  • Point of nomenclature: it isn't a string if it isn't null terminated — by definition of what a string is (C11 §7.1.1 Definitions of terms: A string is a contiguous sequence of characters terminated by and including the first null character. ) There is no such thing as a string that is not null terminated. There are arrays of bytes that are not null terminated but should be.
    – Jonathan Leffler
    Nov 10 at 21:14










  • Note my comment. As a consequence, it would likely be better to use snprintf() and check that the result was not clipped (and decide what to do about it). Or use some sort of variable length memory allocation — a VLA (variable length array) or dynamic allocation via malloc() et al.
    – Jonathan Leffler
    Nov 10 at 21:15











  • what does "null-terminated" mean?
    – simplesystems
    Nov 11 at 7:11










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243301%2finserting-string-variable-inside-a-given-string-in-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













The typical way would be to create a new string by piecing together the three pieces. One way do to this would be the following (shamelessly stolen from the @chux comment):



char buf[1000];
sprintf(buf, “/d1/d2/d3/test%s/d2.xyz”, str);


But before you go that route you need to ensure you really understand the printf family of functions as they are a common source of security related errors. For example, my buf size is large enough for your example, but certainly not for a general solution. Instead the sizes of the input strings would need to be taken into account to ensure the output buffer is large enough.






share|improve this answer




















  • This would also fail if str is not properly null-terminated.
    – Steven W. Klassen
    Nov 10 at 21:09










  • Point of nomenclature: it isn't a string if it isn't null terminated — by definition of what a string is (C11 §7.1.1 Definitions of terms: A string is a contiguous sequence of characters terminated by and including the first null character. ) There is no such thing as a string that is not null terminated. There are arrays of bytes that are not null terminated but should be.
    – Jonathan Leffler
    Nov 10 at 21:14










  • Note my comment. As a consequence, it would likely be better to use snprintf() and check that the result was not clipped (and decide what to do about it). Or use some sort of variable length memory allocation — a VLA (variable length array) or dynamic allocation via malloc() et al.
    – Jonathan Leffler
    Nov 10 at 21:15











  • what does "null-terminated" mean?
    – simplesystems
    Nov 11 at 7:11














up vote
0
down vote













The typical way would be to create a new string by piecing together the three pieces. One way do to this would be the following (shamelessly stolen from the @chux comment):



char buf[1000];
sprintf(buf, “/d1/d2/d3/test%s/d2.xyz”, str);


But before you go that route you need to ensure you really understand the printf family of functions as they are a common source of security related errors. For example, my buf size is large enough for your example, but certainly not for a general solution. Instead the sizes of the input strings would need to be taken into account to ensure the output buffer is large enough.






share|improve this answer




















  • This would also fail if str is not properly null-terminated.
    – Steven W. Klassen
    Nov 10 at 21:09










  • Point of nomenclature: it isn't a string if it isn't null terminated — by definition of what a string is (C11 §7.1.1 Definitions of terms: A string is a contiguous sequence of characters terminated by and including the first null character. ) There is no such thing as a string that is not null terminated. There are arrays of bytes that are not null terminated but should be.
    – Jonathan Leffler
    Nov 10 at 21:14










  • Note my comment. As a consequence, it would likely be better to use snprintf() and check that the result was not clipped (and decide what to do about it). Or use some sort of variable length memory allocation — a VLA (variable length array) or dynamic allocation via malloc() et al.
    – Jonathan Leffler
    Nov 10 at 21:15











  • what does "null-terminated" mean?
    – simplesystems
    Nov 11 at 7:11












up vote
0
down vote










up vote
0
down vote









The typical way would be to create a new string by piecing together the three pieces. One way do to this would be the following (shamelessly stolen from the @chux comment):



char buf[1000];
sprintf(buf, “/d1/d2/d3/test%s/d2.xyz”, str);


But before you go that route you need to ensure you really understand the printf family of functions as they are a common source of security related errors. For example, my buf size is large enough for your example, but certainly not for a general solution. Instead the sizes of the input strings would need to be taken into account to ensure the output buffer is large enough.






share|improve this answer












The typical way would be to create a new string by piecing together the three pieces. One way do to this would be the following (shamelessly stolen from the @chux comment):



char buf[1000];
sprintf(buf, “/d1/d2/d3/test%s/d2.xyz”, str);


But before you go that route you need to ensure you really understand the printf family of functions as they are a common source of security related errors. For example, my buf size is large enough for your example, but certainly not for a general solution. Instead the sizes of the input strings would need to be taken into account to ensure the output buffer is large enough.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 21:08









Steven W. Klassen

22418




22418











  • This would also fail if str is not properly null-terminated.
    – Steven W. Klassen
    Nov 10 at 21:09










  • Point of nomenclature: it isn't a string if it isn't null terminated — by definition of what a string is (C11 §7.1.1 Definitions of terms: A string is a contiguous sequence of characters terminated by and including the first null character. ) There is no such thing as a string that is not null terminated. There are arrays of bytes that are not null terminated but should be.
    – Jonathan Leffler
    Nov 10 at 21:14










  • Note my comment. As a consequence, it would likely be better to use snprintf() and check that the result was not clipped (and decide what to do about it). Or use some sort of variable length memory allocation — a VLA (variable length array) or dynamic allocation via malloc() et al.
    – Jonathan Leffler
    Nov 10 at 21:15











  • what does "null-terminated" mean?
    – simplesystems
    Nov 11 at 7:11
















  • This would also fail if str is not properly null-terminated.
    – Steven W. Klassen
    Nov 10 at 21:09










  • Point of nomenclature: it isn't a string if it isn't null terminated — by definition of what a string is (C11 §7.1.1 Definitions of terms: A string is a contiguous sequence of characters terminated by and including the first null character. ) There is no such thing as a string that is not null terminated. There are arrays of bytes that are not null terminated but should be.
    – Jonathan Leffler
    Nov 10 at 21:14










  • Note my comment. As a consequence, it would likely be better to use snprintf() and check that the result was not clipped (and decide what to do about it). Or use some sort of variable length memory allocation — a VLA (variable length array) or dynamic allocation via malloc() et al.
    – Jonathan Leffler
    Nov 10 at 21:15











  • what does "null-terminated" mean?
    – simplesystems
    Nov 11 at 7:11















This would also fail if str is not properly null-terminated.
– Steven W. Klassen
Nov 10 at 21:09




This would also fail if str is not properly null-terminated.
– Steven W. Klassen
Nov 10 at 21:09












Point of nomenclature: it isn't a string if it isn't null terminated — by definition of what a string is (C11 §7.1.1 Definitions of terms: A string is a contiguous sequence of characters terminated by and including the first null character. ) There is no such thing as a string that is not null terminated. There are arrays of bytes that are not null terminated but should be.
– Jonathan Leffler
Nov 10 at 21:14




Point of nomenclature: it isn't a string if it isn't null terminated — by definition of what a string is (C11 §7.1.1 Definitions of terms: A string is a contiguous sequence of characters terminated by and including the first null character. ) There is no such thing as a string that is not null terminated. There are arrays of bytes that are not null terminated but should be.
– Jonathan Leffler
Nov 10 at 21:14












Note my comment. As a consequence, it would likely be better to use snprintf() and check that the result was not clipped (and decide what to do about it). Or use some sort of variable length memory allocation — a VLA (variable length array) or dynamic allocation via malloc() et al.
– Jonathan Leffler
Nov 10 at 21:15





Note my comment. As a consequence, it would likely be better to use snprintf() and check that the result was not clipped (and decide what to do about it). Or use some sort of variable length memory allocation — a VLA (variable length array) or dynamic allocation via malloc() et al.
– Jonathan Leffler
Nov 10 at 21:15













what does "null-terminated" mean?
– simplesystems
Nov 11 at 7:11




what does "null-terminated" mean?
– simplesystems
Nov 11 at 7:11

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243301%2finserting-string-variable-inside-a-given-string-in-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto