Thread: Best way to move / rename a file in C.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96

    Best way to move / rename a file in C.

    Hello,

    Could someone give me some ideas on what they consider to be the best way to rename / move a file in C? My program will run in Windows and in Linux. The rename() function isn't really an option. I've created a temp file in the temp directory (normally, /tmp in Linux). In a lot of distro's, including mine, /tmp is a different filesystem than the one where the main program resides.

    So far, all I could really think of was to open the tmp file (tempfile), then open the destination file (outfile), and then just read x bytes from tempfile and write x bytes to outfile.

    This is okay for smaller files, but it can cause issues I'd think with larger files.

    I was thinking of trying to use link() and unlink(), however, the link() function seems that it might have the same limitations that rename() has.

    Can anyone think of any other ideas?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Spork Schivago
    The rename() function isn't really an option.
    Why not? It is part of the standard library and so should be available on Windows and Linux.
    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

  3. #3
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by laserlight View Post
    Why not? It is part of the standard library and so should be available on Windows and Linux.
    Thank you for your response Laserlight. The reason rename() won't work is because in Linux, usually the /tmp directory is a different filesystem that's mounted at boot. OpenSUSE does it this way, as does some other distros. rename() won't work across different file systems. It fails with:
    Code:
    EXDEV
    The two file names newname and oldname are on different file systems.
    Seeing how I create the temp file in the /tmp directory, using rename() fails. I believe link() would have the same issue with the filesystem problem.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. Well, you have a choice here, methinks:
    • Use rename, but be prepared to handle EXDEV by doing a copy + remove. You may need to write the code to be compiled conditionally for Linux or Windows. Of course, if the fallback happens, this potentially means a slow down.
    • Create the temporary files in a temporary folder relative to the current working directory instead of /tmp. After all, these files are not necessarily temporary considering that you intend to rename them to something permanent.
    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

  5. #5
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by laserlight View Post
    Ah. Well, you have a choice here, methinks:
    • Use rename, but be prepared to handle EXDEV by doing a copy + remove. You may need to write the code to be compiled conditionally for Linux or Windows. Of course, if the fallback happens, this potentially means a slow down.
    • Create the temporary files in a temporary folder relative to the current working directory instead of /tmp. After all, these files are not necessarily temporary considering that you intend to rename them to something permanent.
    So you're saying try to use rename() / link() and if it fails because the temp file is on a different filesystem, then manually read the temp file and write it to the new file? Back in the 90's, there was a BBS program called JetBBS, written by Troy Beckstrom. When you went to install JetBBS, you had to run a program called JetSetup. JetSetup opened up a file called JetBBS.dat. JetBBS.dat was a custom archive but it used the LHa format, just modified a bit. I do believe I found the original program that Troy used to create this .dat file. It's called ar. Over the years, it evolved and they changed the name to LHarc. The original ar program seems to create the .dat file, exactly the same, if I modify it just a little. The original program created files but had -lh5- and -lh0- inside of them. Troy just changed it so they say -mg5- and -mg0-.

    I appreciate your suggestion laserlight. If I'm understanding your suggestion correctly, this means if a different filesystem is detected, then the program could slow down a good bit when it's copying the temp file. If that's what I gotta do though, that's what I gotta do.

    My configure script gives the user the option of specifying what temp file they'd like to use. I'd really like to keep that in play. I'm thinking of having configure try to auto-detect the OS, and if it's Linux, use /tmp by default (unless the user overrides it), and on Windows, use what ever temp folder Windows uses, by default.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-26-2009, 08:01 PM
  2. speeding up file move/rename
    By marc252 in forum Linux Programming
    Replies: 17
    Last Post: 07-18-2007, 02:14 PM
  3. Rename a file
    By Deb in forum C Programming
    Replies: 3
    Last Post: 04-16-2007, 02:43 PM
  4. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM
  5. rename a file on the C drive
    By cyberpuck52 in forum C Programming
    Replies: 3
    Last Post: 01-20-2002, 01:52 PM