Filling char array with snprintf()
I am trying to fill a char array with values under conditions. The problem is I can not use snprintf()
not properly.
char staedte[MAX_LAENGE_ARR][MAX_LAENGE_STR];
char laender[MAX_LAENGE_ARR][MAX_LAENGE_STR];
int bewohner[MAX_LAENGE_ARR];
char *p = (char*) malloc(len);
if (p == NULL){
perror("malloc failed while allocating an array of chars.");
exit(1);
}
for (int i = 0; i < MAX_LAENGE_ARR; i++) {
if(strcmp(bundesland,laender[i]) == 0 && bewohner[i] >= anzahl){
snprintf(p,MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
snprintf(&p[i],MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
}
}
free(p);
}
c
|
show 8 more comments
I am trying to fill a char array with values under conditions. The problem is I can not use snprintf()
not properly.
char staedte[MAX_LAENGE_ARR][MAX_LAENGE_STR];
char laender[MAX_LAENGE_ARR][MAX_LAENGE_STR];
int bewohner[MAX_LAENGE_ARR];
char *p = (char*) malloc(len);
if (p == NULL){
perror("malloc failed while allocating an array of chars.");
exit(1);
}
for (int i = 0; i < MAX_LAENGE_ARR; i++) {
if(strcmp(bundesland,laender[i]) == 0 && bewohner[i] >= anzahl){
snprintf(p,MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
snprintf(&p[i],MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
}
}
free(p);
}
c
1
bewohner is integer but others are char.
– aomerk
Oct 25 '18 at 16:18
2
Oh duh, eyes deceived me. Please add an example of the file you're reading, the output you expect to see and the output you are currently seeing
– Tormund Giantsbane
Oct 25 '18 at 16:22
i edited my question. my problem is about the usage of char array and snprintf()
– aomerk
Oct 25 '18 at 16:25
2
Please try to post a Minimal, Complete, and Verifiable example. There's a lot of code here that isn't relevant to the problem. I think it will be easier for you to find out what's wrong if you strip the code right down to the bare minimum.
– Tim Randall
Oct 25 '18 at 16:27
1
What doesread_file
return? Usually snprintf is used like:char p[256]; const size_t p_size = 256; const size_t p_pos = 0;
and thenp_pos = snprintf(&p[p_pos], p_size - p_pos, "some string");
. PassingMAX_LAENGE_STR
top
whenp
ismalloc(len);
may overflow. snprintf returns the number of characters written, so you can know when to start the next snprint.
– Kamil Cuk
Oct 25 '18 at 16:57
|
show 8 more comments
I am trying to fill a char array with values under conditions. The problem is I can not use snprintf()
not properly.
char staedte[MAX_LAENGE_ARR][MAX_LAENGE_STR];
char laender[MAX_LAENGE_ARR][MAX_LAENGE_STR];
int bewohner[MAX_LAENGE_ARR];
char *p = (char*) malloc(len);
if (p == NULL){
perror("malloc failed while allocating an array of chars.");
exit(1);
}
for (int i = 0; i < MAX_LAENGE_ARR; i++) {
if(strcmp(bundesland,laender[i]) == 0 && bewohner[i] >= anzahl){
snprintf(p,MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
snprintf(&p[i],MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
}
}
free(p);
}
c
I am trying to fill a char array with values under conditions. The problem is I can not use snprintf()
not properly.
char staedte[MAX_LAENGE_ARR][MAX_LAENGE_STR];
char laender[MAX_LAENGE_ARR][MAX_LAENGE_STR];
int bewohner[MAX_LAENGE_ARR];
char *p = (char*) malloc(len);
if (p == NULL){
perror("malloc failed while allocating an array of chars.");
exit(1);
}
for (int i = 0; i < MAX_LAENGE_ARR; i++) {
if(strcmp(bundesland,laender[i]) == 0 && bewohner[i] >= anzahl){
snprintf(p,MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
snprintf(&p[i],MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
}
}
free(p);
}
c
c
edited Dec 31 '18 at 9:26
aomerk
asked Oct 25 '18 at 16:10
aomerkaomerk
19015
19015
1
bewohner is integer but others are char.
– aomerk
Oct 25 '18 at 16:18
2
Oh duh, eyes deceived me. Please add an example of the file you're reading, the output you expect to see and the output you are currently seeing
– Tormund Giantsbane
Oct 25 '18 at 16:22
i edited my question. my problem is about the usage of char array and snprintf()
– aomerk
Oct 25 '18 at 16:25
2
Please try to post a Minimal, Complete, and Verifiable example. There's a lot of code here that isn't relevant to the problem. I think it will be easier for you to find out what's wrong if you strip the code right down to the bare minimum.
– Tim Randall
Oct 25 '18 at 16:27
1
What doesread_file
return? Usually snprintf is used like:char p[256]; const size_t p_size = 256; const size_t p_pos = 0;
and thenp_pos = snprintf(&p[p_pos], p_size - p_pos, "some string");
. PassingMAX_LAENGE_STR
top
whenp
ismalloc(len);
may overflow. snprintf returns the number of characters written, so you can know when to start the next snprint.
– Kamil Cuk
Oct 25 '18 at 16:57
|
show 8 more comments
1
bewohner is integer but others are char.
– aomerk
Oct 25 '18 at 16:18
2
Oh duh, eyes deceived me. Please add an example of the file you're reading, the output you expect to see and the output you are currently seeing
– Tormund Giantsbane
Oct 25 '18 at 16:22
i edited my question. my problem is about the usage of char array and snprintf()
– aomerk
Oct 25 '18 at 16:25
2
Please try to post a Minimal, Complete, and Verifiable example. There's a lot of code here that isn't relevant to the problem. I think it will be easier for you to find out what's wrong if you strip the code right down to the bare minimum.
– Tim Randall
Oct 25 '18 at 16:27
1
What doesread_file
return? Usually snprintf is used like:char p[256]; const size_t p_size = 256; const size_t p_pos = 0;
and thenp_pos = snprintf(&p[p_pos], p_size - p_pos, "some string");
. PassingMAX_LAENGE_STR
top
whenp
ismalloc(len);
may overflow. snprintf returns the number of characters written, so you can know when to start the next snprint.
– Kamil Cuk
Oct 25 '18 at 16:57
1
1
bewohner is integer but others are char.
– aomerk
Oct 25 '18 at 16:18
bewohner is integer but others are char.
– aomerk
Oct 25 '18 at 16:18
2
2
Oh duh, eyes deceived me. Please add an example of the file you're reading, the output you expect to see and the output you are currently seeing
– Tormund Giantsbane
Oct 25 '18 at 16:22
Oh duh, eyes deceived me. Please add an example of the file you're reading, the output you expect to see and the output you are currently seeing
– Tormund Giantsbane
Oct 25 '18 at 16:22
i edited my question. my problem is about the usage of char array and snprintf()
– aomerk
Oct 25 '18 at 16:25
i edited my question. my problem is about the usage of char array and snprintf()
– aomerk
Oct 25 '18 at 16:25
2
2
Please try to post a Minimal, Complete, and Verifiable example. There's a lot of code here that isn't relevant to the problem. I think it will be easier for you to find out what's wrong if you strip the code right down to the bare minimum.
– Tim Randall
Oct 25 '18 at 16:27
Please try to post a Minimal, Complete, and Verifiable example. There's a lot of code here that isn't relevant to the problem. I think it will be easier for you to find out what's wrong if you strip the code right down to the bare minimum.
– Tim Randall
Oct 25 '18 at 16:27
1
1
What does
read_file
return? Usually snprintf is used like: char p[256]; const size_t p_size = 256; const size_t p_pos = 0;
and then p_pos = snprintf(&p[p_pos], p_size - p_pos, "some string");
. Passing MAX_LAENGE_STR
to p
when p
is malloc(len);
may overflow. snprintf returns the number of characters written, so you can know when to start the next snprint.– Kamil Cuk
Oct 25 '18 at 16:57
What does
read_file
return? Usually snprintf is used like: char p[256]; const size_t p_size = 256; const size_t p_pos = 0;
and then p_pos = snprintf(&p[p_pos], p_size - p_pos, "some string");
. Passing MAX_LAENGE_STR
to p
when p
is malloc(len);
may overflow. snprintf returns the number of characters written, so you can know when to start the next snprint.– Kamil Cuk
Oct 25 '18 at 16:57
|
show 8 more comments
2 Answers
2
active
oldest
votes
snprintf()
returns the number of bytes that it wrote to the string. You can use this to increment the position where you write the next line.
int offset = 0;
for (int i = 0; i < MAX_LAENGE_ARR; i++) {
if(strcmp(bundesland,laender[i]) == 0 && bewohner[i] >= anzahl){
int written = snprintf(p + offset, len - offset, Die Stadt %s hat %d Einwohner. n", staedte[i], bewohner[i]);
printf("%s : %dn",staedte[i] , bewohner[i]);
offset += written;
}
}
I'm not sure why you're calling snprintf()
twice. I removed the one that just writes to p
.
Since the length of the p
string is len
bytes, you should use that when specifying the maximum amount to write in snprintf()
, not MAX_LANGE_STR
. You have to subtract offset
from it, since each write is further in the string and there's less room left after it.
I wasnt actually calling it twice, I was just showing two different ways I was trying :)
– aomerk
Oct 25 '18 at 19:15
add a comment |
regarding:
if( strcmp( bundesland,laender[i] ) == 0 && bewohner[i] >= anzahl)
{
snprintf(p,MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
snprintf(&p[i],MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
printf("%s : %dn",staedte[i] , bewohner[i]);
}
the first call to snprintf()
keeps overlaying the first entry in the p
array
suggest:
char buffer[MAX_LAENGE_STR+1];
p[0] = '';
then in the loop
if( strcmp( bundesland,laender[i] ) == 0 && bewohner[i] >= anzahl)
{
snprintf( buffer, MAX_LAENGE_STR, "Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
strcat( p, buffer );
printf("%s : %dn",staedte[i] , bewohner[i]);
}
I'll leave it to you to add the checking that the buffer p
is not overflowed.
p
is not an array of pointers, it's a pointer to achar
array.
– Barmar
Oct 25 '18 at 17:57
1
@Barmar. Ok, I'll modify my answer accordingly
– user3629249
Oct 25 '18 at 18:01
but this way, there is always a empty line at the beginning.
– aomerk
Oct 25 '18 at 20:25
@aomerk, no, the use ofstrcat()
will place the data at the next available location inp
– user3629249
Oct 25 '18 at 20:54
@aomerk, I have no idea what the 'Bug' is referring to. The question states that there are 8 lines to be printed
– user3629249
Oct 25 '18 at 21:00
|
show 3 more comments
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f52993788%2ffilling-char-array-with-snprintf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
snprintf()
returns the number of bytes that it wrote to the string. You can use this to increment the position where you write the next line.
int offset = 0;
for (int i = 0; i < MAX_LAENGE_ARR; i++) {
if(strcmp(bundesland,laender[i]) == 0 && bewohner[i] >= anzahl){
int written = snprintf(p + offset, len - offset, Die Stadt %s hat %d Einwohner. n", staedte[i], bewohner[i]);
printf("%s : %dn",staedte[i] , bewohner[i]);
offset += written;
}
}
I'm not sure why you're calling snprintf()
twice. I removed the one that just writes to p
.
Since the length of the p
string is len
bytes, you should use that when specifying the maximum amount to write in snprintf()
, not MAX_LANGE_STR
. You have to subtract offset
from it, since each write is further in the string and there's less room left after it.
I wasnt actually calling it twice, I was just showing two different ways I was trying :)
– aomerk
Oct 25 '18 at 19:15
add a comment |
snprintf()
returns the number of bytes that it wrote to the string. You can use this to increment the position where you write the next line.
int offset = 0;
for (int i = 0; i < MAX_LAENGE_ARR; i++) {
if(strcmp(bundesland,laender[i]) == 0 && bewohner[i] >= anzahl){
int written = snprintf(p + offset, len - offset, Die Stadt %s hat %d Einwohner. n", staedte[i], bewohner[i]);
printf("%s : %dn",staedte[i] , bewohner[i]);
offset += written;
}
}
I'm not sure why you're calling snprintf()
twice. I removed the one that just writes to p
.
Since the length of the p
string is len
bytes, you should use that when specifying the maximum amount to write in snprintf()
, not MAX_LANGE_STR
. You have to subtract offset
from it, since each write is further in the string and there's less room left after it.
I wasnt actually calling it twice, I was just showing two different ways I was trying :)
– aomerk
Oct 25 '18 at 19:15
add a comment |
snprintf()
returns the number of bytes that it wrote to the string. You can use this to increment the position where you write the next line.
int offset = 0;
for (int i = 0; i < MAX_LAENGE_ARR; i++) {
if(strcmp(bundesland,laender[i]) == 0 && bewohner[i] >= anzahl){
int written = snprintf(p + offset, len - offset, Die Stadt %s hat %d Einwohner. n", staedte[i], bewohner[i]);
printf("%s : %dn",staedte[i] , bewohner[i]);
offset += written;
}
}
I'm not sure why you're calling snprintf()
twice. I removed the one that just writes to p
.
Since the length of the p
string is len
bytes, you should use that when specifying the maximum amount to write in snprintf()
, not MAX_LANGE_STR
. You have to subtract offset
from it, since each write is further in the string and there's less room left after it.
snprintf()
returns the number of bytes that it wrote to the string. You can use this to increment the position where you write the next line.
int offset = 0;
for (int i = 0; i < MAX_LAENGE_ARR; i++) {
if(strcmp(bundesland,laender[i]) == 0 && bewohner[i] >= anzahl){
int written = snprintf(p + offset, len - offset, Die Stadt %s hat %d Einwohner. n", staedte[i], bewohner[i]);
printf("%s : %dn",staedte[i] , bewohner[i]);
offset += written;
}
}
I'm not sure why you're calling snprintf()
twice. I removed the one that just writes to p
.
Since the length of the p
string is len
bytes, you should use that when specifying the maximum amount to write in snprintf()
, not MAX_LANGE_STR
. You have to subtract offset
from it, since each write is further in the string and there's less room left after it.
answered Oct 25 '18 at 18:01
BarmarBarmar
426k36249351
426k36249351
I wasnt actually calling it twice, I was just showing two different ways I was trying :)
– aomerk
Oct 25 '18 at 19:15
add a comment |
I wasnt actually calling it twice, I was just showing two different ways I was trying :)
– aomerk
Oct 25 '18 at 19:15
I wasnt actually calling it twice, I was just showing two different ways I was trying :)
– aomerk
Oct 25 '18 at 19:15
I wasnt actually calling it twice, I was just showing two different ways I was trying :)
– aomerk
Oct 25 '18 at 19:15
add a comment |
regarding:
if( strcmp( bundesland,laender[i] ) == 0 && bewohner[i] >= anzahl)
{
snprintf(p,MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
snprintf(&p[i],MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
printf("%s : %dn",staedte[i] , bewohner[i]);
}
the first call to snprintf()
keeps overlaying the first entry in the p
array
suggest:
char buffer[MAX_LAENGE_STR+1];
p[0] = '';
then in the loop
if( strcmp( bundesland,laender[i] ) == 0 && bewohner[i] >= anzahl)
{
snprintf( buffer, MAX_LAENGE_STR, "Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
strcat( p, buffer );
printf("%s : %dn",staedte[i] , bewohner[i]);
}
I'll leave it to you to add the checking that the buffer p
is not overflowed.
p
is not an array of pointers, it's a pointer to achar
array.
– Barmar
Oct 25 '18 at 17:57
1
@Barmar. Ok, I'll modify my answer accordingly
– user3629249
Oct 25 '18 at 18:01
but this way, there is always a empty line at the beginning.
– aomerk
Oct 25 '18 at 20:25
@aomerk, no, the use ofstrcat()
will place the data at the next available location inp
– user3629249
Oct 25 '18 at 20:54
@aomerk, I have no idea what the 'Bug' is referring to. The question states that there are 8 lines to be printed
– user3629249
Oct 25 '18 at 21:00
|
show 3 more comments
regarding:
if( strcmp( bundesland,laender[i] ) == 0 && bewohner[i] >= anzahl)
{
snprintf(p,MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
snprintf(&p[i],MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
printf("%s : %dn",staedte[i] , bewohner[i]);
}
the first call to snprintf()
keeps overlaying the first entry in the p
array
suggest:
char buffer[MAX_LAENGE_STR+1];
p[0] = '';
then in the loop
if( strcmp( bundesland,laender[i] ) == 0 && bewohner[i] >= anzahl)
{
snprintf( buffer, MAX_LAENGE_STR, "Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
strcat( p, buffer );
printf("%s : %dn",staedte[i] , bewohner[i]);
}
I'll leave it to you to add the checking that the buffer p
is not overflowed.
p
is not an array of pointers, it's a pointer to achar
array.
– Barmar
Oct 25 '18 at 17:57
1
@Barmar. Ok, I'll modify my answer accordingly
– user3629249
Oct 25 '18 at 18:01
but this way, there is always a empty line at the beginning.
– aomerk
Oct 25 '18 at 20:25
@aomerk, no, the use ofstrcat()
will place the data at the next available location inp
– user3629249
Oct 25 '18 at 20:54
@aomerk, I have no idea what the 'Bug' is referring to. The question states that there are 8 lines to be printed
– user3629249
Oct 25 '18 at 21:00
|
show 3 more comments
regarding:
if( strcmp( bundesland,laender[i] ) == 0 && bewohner[i] >= anzahl)
{
snprintf(p,MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
snprintf(&p[i],MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
printf("%s : %dn",staedte[i] , bewohner[i]);
}
the first call to snprintf()
keeps overlaying the first entry in the p
array
suggest:
char buffer[MAX_LAENGE_STR+1];
p[0] = '';
then in the loop
if( strcmp( bundesland,laender[i] ) == 0 && bewohner[i] >= anzahl)
{
snprintf( buffer, MAX_LAENGE_STR, "Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
strcat( p, buffer );
printf("%s : %dn",staedte[i] , bewohner[i]);
}
I'll leave it to you to add the checking that the buffer p
is not overflowed.
regarding:
if( strcmp( bundesland,laender[i] ) == 0 && bewohner[i] >= anzahl)
{
snprintf(p,MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
snprintf(&p[i],MAX_LAENGE_STR,"Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
printf("%s : %dn",staedte[i] , bewohner[i]);
}
the first call to snprintf()
keeps overlaying the first entry in the p
array
suggest:
char buffer[MAX_LAENGE_STR+1];
p[0] = '';
then in the loop
if( strcmp( bundesland,laender[i] ) == 0 && bewohner[i] >= anzahl)
{
snprintf( buffer, MAX_LAENGE_STR, "Die Stadt %s hat %d Einwohner. n", staedte[i],bewohner[i]);
strcat( p, buffer );
printf("%s : %dn",staedte[i] , bewohner[i]);
}
I'll leave it to you to add the checking that the buffer p
is not overflowed.
edited Oct 25 '18 at 18:15
answered Oct 25 '18 at 17:56
user3629249user3629249
11.1k11014
11.1k11014
p
is not an array of pointers, it's a pointer to achar
array.
– Barmar
Oct 25 '18 at 17:57
1
@Barmar. Ok, I'll modify my answer accordingly
– user3629249
Oct 25 '18 at 18:01
but this way, there is always a empty line at the beginning.
– aomerk
Oct 25 '18 at 20:25
@aomerk, no, the use ofstrcat()
will place the data at the next available location inp
– user3629249
Oct 25 '18 at 20:54
@aomerk, I have no idea what the 'Bug' is referring to. The question states that there are 8 lines to be printed
– user3629249
Oct 25 '18 at 21:00
|
show 3 more comments
p
is not an array of pointers, it's a pointer to achar
array.
– Barmar
Oct 25 '18 at 17:57
1
@Barmar. Ok, I'll modify my answer accordingly
– user3629249
Oct 25 '18 at 18:01
but this way, there is always a empty line at the beginning.
– aomerk
Oct 25 '18 at 20:25
@aomerk, no, the use ofstrcat()
will place the data at the next available location inp
– user3629249
Oct 25 '18 at 20:54
@aomerk, I have no idea what the 'Bug' is referring to. The question states that there are 8 lines to be printed
– user3629249
Oct 25 '18 at 21:00
p
is not an array of pointers, it's a pointer to a char
array.– Barmar
Oct 25 '18 at 17:57
p
is not an array of pointers, it's a pointer to a char
array.– Barmar
Oct 25 '18 at 17:57
1
1
@Barmar. Ok, I'll modify my answer accordingly
– user3629249
Oct 25 '18 at 18:01
@Barmar. Ok, I'll modify my answer accordingly
– user3629249
Oct 25 '18 at 18:01
but this way, there is always a empty line at the beginning.
– aomerk
Oct 25 '18 at 20:25
but this way, there is always a empty line at the beginning.
– aomerk
Oct 25 '18 at 20:25
@aomerk, no, the use of
strcat()
will place the data at the next available location in p
– user3629249
Oct 25 '18 at 20:54
@aomerk, no, the use of
strcat()
will place the data at the next available location in p
– user3629249
Oct 25 '18 at 20:54
@aomerk, I have no idea what the 'Bug' is referring to. The question states that there are 8 lines to be printed
– user3629249
Oct 25 '18 at 21:00
@aomerk, I have no idea what the 'Bug' is referring to. The question states that there are 8 lines to be printed
– user3629249
Oct 25 '18 at 21:00
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f52993788%2ffilling-char-array-with-snprintf%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
bewohner is integer but others are char.
– aomerk
Oct 25 '18 at 16:18
2
Oh duh, eyes deceived me. Please add an example of the file you're reading, the output you expect to see and the output you are currently seeing
– Tormund Giantsbane
Oct 25 '18 at 16:22
i edited my question. my problem is about the usage of char array and snprintf()
– aomerk
Oct 25 '18 at 16:25
2
Please try to post a Minimal, Complete, and Verifiable example. There's a lot of code here that isn't relevant to the problem. I think it will be easier for you to find out what's wrong if you strip the code right down to the bare minimum.
– Tim Randall
Oct 25 '18 at 16:27
1
What does
read_file
return? Usually snprintf is used like:char p[256]; const size_t p_size = 256; const size_t p_pos = 0;
and thenp_pos = snprintf(&p[p_pos], p_size - p_pos, "some string");
. PassingMAX_LAENGE_STR
top
whenp
ismalloc(len);
may overflow. snprintf returns the number of characters written, so you can know when to start the next snprint.– Kamil Cuk
Oct 25 '18 at 16:57