I am new to C. I have been searching for hours to find an answer to this problem but no luck. Your help would be greatly appreciated.
I need to write a function that takes the file path as argument (I am calling this argument WorkingDir below)
This function
void test1(char *WorkingDir)
{
FILE *out_file1;
out_file1 = fopen(strcat(WorkingDir,"Th.txt"), "wt");
// the above attempts to open file /WorkingDir/Th.txt
fclose(out_file1);
}
called as
test1("/my/directory/")
does not work (it does not set the path as required), although this one works just fine
void test2(char *WorkingDir) #argument is not used anywhere
{
char path[100]="/my/directory/";
FILE *out_file1;
out_file1 = fopen(strcat(path,"Th.txt"), "wt");
fclose(out_file1);
}
Thank you all for your answers. An important detail that I did not mention is that I am calling the C function from R. To pass a string from R to C requires char ** argument. So this function sets the path as required:
void test101(char **WorkingDir){
const int MAX_PATH = 300;
char path_name[MAX_PATH + 1];
snprintf(path_name,MAX_PATH,"%s%s",*WorkingDir,"Th.txt");
}
The above function uses your inputs. Thank you for them.