Thread: Problems passing file path names with spaces into function

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    16

    Problems passing file path names with spaces into function

    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!

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It is not "bad practice" to have spaces in file names. All programs should be able to handle spaces in file names.

    You can't just add quotes to the file name. Do you think the quotes are part of the file name in your first example fopen call? They simply delimit the string. Have you tried it without adding the quotes? What is the result?

    What is the exact contents of "test.txt" with this:
    Code:
    void dummy_function(char **out) {
             FILE *f = fopen("test.txt", "w");
             fprintf(f, "[%s]\n", *out);
             fclose(f);
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This R?
    R: What is R?

    > 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
    First of all, does this literal string work?
    FILE *MyFile = fopen("C:/Project/My Folder/output.csv","w");

    If it does, then the problem is in the way R does it's space quoting and has absolutely nothing to do with C.

    For example, in a bash shell, one would have to type
    C:/Project/My\ Folder/output.csv

    You really should be able to use a debugger to find out what string "with spaces" was passed into
    void dummy_function(char **my_output_file)

    My guess is, due to R's parsing rules, all you got was "C:/Project/My" (truncated at the first space).

    In which case, you need to read R's documentation to find out how to escape special characters like space.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Hi Salem,
    The literal string works absolutely fine and it look like the entire string is passing through the function. That's why I thought trying to paste quotes around it might work, but there must be some other weird formatting issue with the space. There is unfortunately not much useful documentation for this particular R to C problem.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by NotAProgrammer View Post
    (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); 
    }
    From looking at the function signature, I would just guess that you're supposed to tell R which file you created by using the "my_output_file" argument. This would be a so-called "output" argument. For example, try this and see what happens when you call it from R.

    Code:
    void dummy_function(char **my_output_file)
    {
        // Create foo.txt. 
        FILE* fp = fopen("C:\\foo.txt", "w");
        fprintf(fp, "Hello. This is foo.txt.\n");
        fclose(fp);
    
        // Return path to R. 
        #define SZ 300
        char *path = malloc(SZ);
        path[SZ-1] = 0;
        strncpy(path, "C:\\foo.txt", SZ);
        if (path[SZ-1] != 0) {
             fprintf(stderr, "path buffer is too small\n");
            *my_output_file = NULL;
            return;
        }
        *my_output_file = &path;
    }
    Try it and see if C:\foo.txt actually gets created. You should probably also check for errors and use perror to print the error message to stderr if something goes wrong (e.g. disk full, no permissions, etc.). If it works, try some other experiments, like a path with spaces, etc.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Hi Algorism,
    The exact output is [C:/Project/My Folder/output.csv] so it looks like there are no weird formatting characters with the spaces. How do I get C to recognize the string as a file path name?
    Thanks

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Hi c99tutorial,
    As far as I can tell, when I type:
    Code:
    FILE* fp = fopen("C:/Project/My Folder/output.csv", "w");
        fprintf(fp, "Hello. This is foo.csv.\n");
        fclose(fp);
    It writes the text into the file just fine.

    However, when I try
    Code:
    char path[300];
    strcpy(path, *my_output_file);
    FILE *MyFile = fopen(path,"w"); 
      if(MyFile == NULL)
         {
               Rprintf("Error");
         }
    fclose(MyFile);
    It can't open the file and returns the Error message to R. I checked in the response to Algorism to make sure there were no weird formatting things in the string, but it appears to be correct. Does it need to have quotes around it for C to recognize it as a path with spaces?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe you should check that the value of *my_output_file is equal to "C:/Project/My Folder/output.csv", either by checking with a debugger, printing it, or by using strcmp.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by NotAProgrammer View Post
    Hi Algorism,
    The exact output is [C:/Project/My Folder/output.csv] so it looks like there are no weird formatting characters with the spaces. How do I get C to recognize the string as a file path name?
    Thanks
    If that worked, then this should work:
    Code:
    void dummy_function(char **my_output_file) {   
        FILE *MyFile = fopen(*my_output_file, "w"); 
        fprintf(MyFile, "%d,%d\n", 5, 6);
        fclose(MyFile); 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File path problems
    By drclark in forum C Programming
    Replies: 1
    Last Post: 08-17-2011, 01:41 AM
  2. FAT32 volumes: How to get 8.3 short path and file names?
    By Morkel in forum Linux Programming
    Replies: 3
    Last Post: 05-22-2011, 11:11 PM
  3. Formatting file path with spaces
    By Opariti in forum C Programming
    Replies: 16
    Last Post: 08-19-2009, 04:16 AM
  4. using path names
    By AmazingRando in forum C Programming
    Replies: 4
    Last Post: 09-23-2003, 06:09 PM
  5. Passing file names
    By PJYelton in forum C++ Programming
    Replies: 1
    Last Post: 12-27-2002, 12:08 PM

Tags for this Thread