Hi all,
I am writing a C function that can be called through R and am running into problems with passing file path name strings that contain spaces. I know it is bad practice to have spaces in path names, but I am writing the program for other non-computer folks to use and so it has to handle spaces.
Example path name: C:/Project/My Folder/output.csv
If I type the path name in quotes directly into the test function below, it works fine. (The char ** argument for the function is a quirk of using the .C call in R to pass strings).
Code:
void dummy_function(char **my_output_file)
{
char path[300];
strcpy(path, *my_output_file);
FILE *MyFile = fopen("C:/Project/My Folder/output.csv","w");
fprintf(MyFile, "%d,%d\n", 5,6);
fclose(MyFile);
}
I tried a very hack way of pasting quotes around the input text string. It prints back console as "C:/Project/My Folder/output.csv", but it is not recognized by fopen().
Code:
void dummy_function(char **my_output_file)
{
char path[300];
strcpy(path, *my_output_file);
char start_quotes[300] = "\"";
strcat(start_quotes, path);
char end_quotes[300] = "\"";
strcat(start_quotes, end_quotes);
FILE *MyFile = fopen(start_quotes,"w");
fprintf(MyFile, "%d,%d\n", 5,6);
fclose(MyFile);
}
Is there a better way to make this work?
Thanks!