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?
c string path
add a comment |
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?
c string path
1
Researchsnprintf(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 writtenchar *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
add a comment |
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?
c string path
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
c string path
asked Nov 10 at 20:52
simplesystems
3001317
3001317
1
Researchsnprintf(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 writtenchar *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
add a comment |
1
Researchsnprintf(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 writtenchar *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
add a comment |
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.
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 usesnprintf()
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 viamalloc()
et al.
– Jonathan Leffler
Nov 10 at 21:15
what does "null-terminated" mean?
– simplesystems
Nov 11 at 7:11
add a comment |
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.
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 usesnprintf()
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 viamalloc()
et al.
– Jonathan Leffler
Nov 10 at 21:15
what does "null-terminated" mean?
– simplesystems
Nov 11 at 7:11
add a comment |
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.
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 usesnprintf()
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 viamalloc()
et al.
– Jonathan Leffler
Nov 10 at 21:15
what does "null-terminated" mean?
– simplesystems
Nov 11 at 7:11
add a comment |
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.
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.
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 usesnprintf()
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 viamalloc()
et al.
– Jonathan Leffler
Nov 10 at 21:15
what does "null-terminated" mean?
– simplesystems
Nov 11 at 7:11
add a comment |
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 usesnprintf()
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 viamalloc()
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 writtenchar *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