I am new to C program. I want to copy a string from "txtFilename" to "s1". But I don't know how many characters are there in "txtFilename" in advance. I set the "txtFilename"'s array size to be 100. Actually the characters stored in txtFilename is always <100. Therefore, I set "s1"'s array size to be 100 also.
My goal is I want to copy all the characters from "txtFilename" which is less than 100 size to "s1" and append some characters to s1 without getting warning or segmentation fault or memory overflow. My goal is storing the memory of s1 only to the size of the character it is concatenated or copied even I set "s1" to 100. (eg. if the final s1="abcde12345", even I set s1[100], I want to set s1 to be 11 automatically to avoid memory overflow.)
I wrote the program according to the following:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{ char s1[100]="";
char txtFilename[100];
char t[100];
char m[100];
strcpy(txtFilename, "File_data_checking_415464324_444543.");
snprintf(s1,sizeof(s1),"%scsv",txtFilename);
printf("%s\n",s1);
strcpy(txtFilename,s1);
printf("%s\n",txtFilename);
snprintf(t,sizeof(t),"%s_sfewf",s1);
printf("%s\n",t);
return 0;
}
I got this warning. May I know how can I solve this warning?
main.c: In function ‘main’:
main.c:19:31: warning: ‘csv’ directive output may be truncated writing 3 bytes into a region of size between 1 and 100 [-Wformat-truncation=]
19 | snprintf(s1,sizeof(s1),"%scsv",txtFilename);
| ^~~
main.c:19:5: note: ‘snprintf’ output between 4 and 103 bytes into a destination of size 100
19 | snprintf(s1,sizeof(s1),"%scsv",txtFilename);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:23:29: warning: ‘_sfewf’ directive output may be truncated writing 6 bytes into a region of size between 1 and 100 [-Wformat-truncation=]
23 | snprintf(t,sizeof(t),"%s_sfewf",s1);
| ^~~~~~
main.c:23:5: note: ‘snprintf’ output between 7 and 106 bytes into a destination of size 100
23 | snprintf(t,sizeof(t),"%s_sfewf",s1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
File_data_checking_415464324_444543.csv
File_data_checking_415464324_444543.csv
File_data_checking_415464324_444543.csv_sfewf
CodePudding user response:
Sometimes you need to look for alternative solutions.
#include <stdio.h>
#include <string.h>
int main( void ) {
char s1[ 128 ]; // big enough...
strcpy( s1, "File_data_checking_415464324_444543" );
strcat( s1, "." );
strcat( s1, "csv" );
printf( "%s\n", s1 );
printf( "len = %zu\n", strlen( s1 ) );
printf( "sizeof(s1) = %zu\n", sizeof(s1) );
return 0;
}
CodePudding user response:
A pointer could be used instead of an array.
Call snprintf
once to get the needed size.
Allocate memory to the pointer and call snprintf
again.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void)
{
char *s1=NULL;
char txtFilename[100];
strcpy(txtFilename,"File_data_checking_415464324_444543.");
int size = snprintf(NULL,0,"%scsv",txtFilename);
size; // for terminating zero
if ( NULL != ( s1 = malloc ( size))) {
snprintf(s1,size,"%scsv",txtFilename);
strcpy(txtFilename,s1);
printf("%s\n",txtFilename);
free ( s1);
}
else {
fprintf ( stderr, "problem malloc\n");
return 1;
}
return 0;
}