Copy files in to a different folder help

This is a discussion on Copy files in to a different folder help within the C Programming forums, part of the General Programming Boards category; Hey guys I'm having troubles figuring this out. I am trying to copy a bunch of files in a directory ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    Copy files in to a different folder help

    Hey guys I'm having troubles figuring this out. I am trying to copy a bunch of files in a directory to a different here's what i have

    Code:
    
    if ((from = fopen(filepath, "rb")) == NULL) {
    				printf("Cannot open source file.\n");
    				exit(1);
    			}
    
    			//printf("opened source file\n");
    
    			// open destination file 
    			if ((to = fopen(backupdir, "wb")) == NULL) {
    				printf("Cannot open destination file.\n");
    				exit(1);
    			}
    this is my error when i try to copy from /home/TEST to /home/1231232141243

    New directory /home/1231232141243 created successfully.
    Backup file dir: /home/1231232141243/Copyof-1.txt/
    Cannot open destination file.
    Frustrating because I got it to work and didn't think it worked and so deleted some code :\

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You are trying to open a subdirectory that hasn't been created? If you just created /home/1231232141243, then you could write /home/1231232141243/Copyof-1.txt , but you cannot write /home/1231232131243/Copyof-1.txt/somefile, since that intermediate directory doesn't exist.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    ok so. here's what I have figured out.

    If the directory doesn't exist, the program creates the directory, but it does not put the files in the newly created directory. Instead it puts the files into the current directory where the program is being ran.

    Then if I run it again the files go into the directory in which I created earlier (meaning it works). Do i throw in a sleep or something to fix this?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    OMG shoot me for being dumb. Sorry for wasting your guys' time. I didn't see that extra '/' that somehow snuck its way into my string concat . . . thanks for that tab.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Note fopen() is for opening files not directories.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX 9.0c Common Files folder
    By Swordsman in forum Windows Programming
    Replies: 2
    Last Post: 07-28-2008, 08:56 AM
  2. get files in a folder and more
    By appleGuy in forum Windows Programming
    Replies: 3
    Last Post: 09-05-2006, 08:59 AM
  3. function to copy file from one folder to another
    By Harman in forum C Programming
    Replies: 7
    Last Post: 05-15-2005, 11:39 AM
  4. how to get names of files in folder
    By kiku in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2005, 05:51 PM
  5. ...how to get the number of files in a folder
    By toby1909 in forum Windows Programming
    Replies: 5
    Last Post: 02-01-2002, 04:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21