Thread: Moving a file to a directory containing spaces

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    15

    Moving a file to a directory containing spaces

    I have a "string," if you will, of a directory where I am going to move a file over, I've declared it as:

    Code:
    char duplicate_file[] = "C:\\Program Files\\File Manager\\";
    After I get the original file's name, I want to move the file to it's new location. I do this with the following code:

    Code:
    /*Copy the file */
    	org = fopen(original_file,"r");
    	dup = fopen(duplicate_file,"w");
    	if( org == NULL || dup == NULL)
    		error_exit("Error opening or creating file");
    
    	while( (ch=fgetc(org)) != EOF )
    		fputc(ch,dup);
    
    	fclose(org);
    	fclose(dup);
    The program works fine when my "duplicate_file" directory does not contain any spaces, so my question is how can I make this program work with the spaces?

    Thanks a bunch for any input!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are you compiling the code with a win32 compiler, which would probably work.
    Or some ancient fossil DOS compiler which won't stand a chance.
    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.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by notsocks View Post
    The program works fine when my "duplicate_file" directory does not contain any spaces, so my question is how can I make this program work with the spaces?
    The language is completely agnostic about the contents of pathnames. If spaces in the path are causing problems, it's due to some operating system or system library limitation. Sadly, not much can be done about that aside from switching to a different C library or ditching the buggy platform altogether. I know that spaces in pathnames are not a problem on modern Windows, so I'd point fingers at the library being at fault.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you might want to try using 8.3 filenames or quotes like this:

    Code:
    char duplicate_file[] = "\"C:\\Program Files\\File Manager\\\"";

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    15
    Thanks for the responses!

    (I'm using Dev-C++ 4.9.9.2)

    The language is completely agnostic about the contents of pathnames. If spaces in the path are causing problems, it's due to some operating system or system library limitation. Sadly, not much can be done about that aside from switching to a different C library or ditching the buggy platform altogether. I know that spaces in pathnames are not a problem on modern Windows, so I'd point fingers at the library being at fault.
    I figured it might be a problem with the library... is there a different library I could use then? The ones I am trying to use are as follows:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    Not sure where to go from here.

    Thanks!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Dunno - works as expected here.

    Perhaps it's the trailing \ of your duplicate path.

    Perhaps use perror() to discover the real reason for fopen() returning NULL.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    15
    I made some changes and experimented, but it's still not working. I've tried to put the entire path "C:\Program Files\File Mananger\File.txt" in quotes, and without (thanks for the idea robwhit), but neither way seems to affect it.

    Code:
    /* declare variables */
    	char original_file[256];
    	   //char duplicate_file[256] = "\"C:\\Program Files\\File Manager\\";
    	char duplicate_file[256] = "C:\\Program Files\\File Mananger\\";
    	FILE *org,*dup;
    	char ch;
    
    /* gather the file's name */
    	printf("Enter the name of the original file: ");
    	scanf("&#37;255s",&original_file);
    	fflush(stdin);
    	if(strlen(original_file) == 0)
    		error_exit("Missing filename");
    	
           //strcat(original_file,"\"");           //Add a quote at the end of the original file's name.
    
           strcat(duplicate_file,original_file);     //Combine the destination directory and the file's name to make a copy of it.
    
    /*Copy the file */
    	org = fopen(original_file,"r");
    	dup = fopen(duplicate_file,"w");
    	if( org == NULL || dup == NULL)
    		error_exit("Error opening or creating file");
    
    	while( (ch=fgetc(org)) != EOF )
            fputc(ch,dup);
    That is my chunk of code that is supposed to get the file's name and then move it to C:\Program Files\File Mananger. Still, works fine without the spaces but not with them. I'm really stumped here...


  8. #8
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    :-)

    fflush(stdin);
    isn't this a problem~

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    15
    isn't this a problem~
    I don't see how it could be, I need it in my program so the enter doesn't bypass my getchar();s that I've placed in the program so the user can see what's going on (becuase otherwise it'll run and close in an instant). Unless it's doing something other than that (and I did try commenting it out, but nothing changed except that the program ran and closed itself in less than a second) I don't think it's the root of the problem. Thanks for the suggestion, though.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by notsocks View Post
    I don't see how it could be, I need it in my program so the enter doesn't bypass my getchar();s that I've placed in the program so the user can see what's going on (becuase otherwise it'll run and close in an instant). Unless it's doing something other than that (and I did try commenting it out, but nothing changed except that the program ran and closed itself in less than a second) I don't think it's the root of the problem. Thanks for the suggestion, though.
    It is a problem because it's not defined to do what you want by any C standard. If it works, then it's because your compiler has extended the functionality of fflush() to allow it to work.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    So if you want to use it, that's up to you, but it's not portable, and we cannot be sure it will work with any particular system.

    For alternatives:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    If you want, you can write your own OS specific version. I wrote one for Windows somewhere, although I don't think it fully works for C I/O (ie. it works if you're only using the Win32 functions for I/O), but I imagine the one inside the link should be sufficient for you.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf("&#37;255s",&original_file);
    1. Drop the &. original_file is a char array already.
    2. %s conversions are delimited by spaces, so it's impossible to read "hello world" with a single %s conversion. Use a different conversion, say "%255[^\n]"
    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.

  12. #12
    Registered User
    Join Date
    Jun 2007
    Posts
    15
    Salem, that last bit did the trick! The file now moves to that directory without a problem!!! YAY!

    And albert3721 and Macgyver, thanks for that heads up. I stand corrected! I'll look into that right away.

    Thanks a bunch, guys! I really appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM