I have this code where I take a file name and generate a new file with the same file name but ending in .c
However, when I attempt to transform the file name to end in a .c, I try to print the result to check if it's working. And printf just prints nothing. If it makes a difference, the file name is a pointer that points to argv[1] from int main(int argc, char *argv[]).
Here is the code:
bool file_genEquiCFile(char *file_name)
{
// Maybe add a memmove to preserve the original buffer
printf("file_name before change: %s\n", file_name);
while (file_name && *file_name != '.')
file_name++;
if (file_name)
{
if (*file_name++ == '.')
{
*file_name++ = 'c';
*file_name = '\0';
// For now use "w" mode which makes an empty file for writing.
FILE *c_file = fopen(file_name, "w");
fclose(c_file);
printf("file_name after change: %s\n", file_name);
return true;
}
else
return false;
}
return false;
}
By the way, file_name points to a file called hey.txt Here is what printf printed:
file_name before change: hey.txt
file_name after change: