I am new to C this line of code seems to be causing a segfault based on debugging. valgrind says that there is something wrong with it calling upon uninitialized values. I assume it doesn't like how I made the Random integer to string stuff.
int main(int argc, char** argv){
srand(time(NULL));
printf("main now \n");
fflush(stdout);
if (argc >= 3)
{
printf("Too many files!\n");
fflush(stdout);
return EXIT_FAILURE;
}
else{
//Actual main part
//Setting up random
int choice2; //user's choice
int breaker = 1;
printf("elsecheck \n");
fflush(stdout);
int randomnum = rand();
//Convert random integer into a string
char stringrandom[6] = {'0'};
printf("2 \n");
fflush(stdout);
////
sprintf(randomnum, stringrandom, 10);
fflush(stdout);
//////
printf("random \n");
fflush(stdout);
//Onid for directory creation
char onid[10] = "chankevi";
//FD for file check
int fd;
//Strings for name of file
char filename[1000];
char* newFilePath;
CodePudding user response:
In your code the usage of string printf sprintf
is incorrect.
You probably want something like:
sprintf(&stringrandom,"%d", randomnum);
But actually you should not use sprintf
at all since it is very prone to buffer overruns as there are no checks that the allocated buffer is long enough
https://linux.die.net/man/3/sprintf
CodePudding user response:
Thanks for the help simson turns out it was me not realizing that sprintf had a different format than itoa