What is the best way to build the absolute path to the file and then create a directory and a file? I have found the following method. Could you please review it, or suggest something better?
char *pub_working_dir, *pub_results_dir, *pub_path_file;
pub_working_dir=(char *)calloc(256, sizeof(char));
if (pub_working_dir) buffer=_getcwd(pub_working_dir, 256);
if (buffer!=NULL)
{
pub_results_dir=(char *)calloc(256, sizeof(char));
if (pub_results_dir)
{
sprintf_s(pub_results_dir, 256, "%s\\results", pub_working_dir);
_mkdir(pub_results_dir);
pub_path_file=(char *)calloc(256, sizeof(char));
if (pub_path_file)
{
sprintf_s(pub_path_file, 256, "%s\\file.txt", pub_results_dir);
FILE *file;
int err_fp = fopen_s(&file, pub_path_file, "a");
if (err_fp==0)
{
// write to the file
fclose(file);
}
}
}
}
EDIT: I am looking for the fastest and most readable method purposed to work with Visual Studio 2022 Compiler on Windows.