Utility for copying and renaming files.

Utility for copying and renaming files.

FileUtils.h

Committer:
ollie8
Date:
2013-12-30
Revision:
1:1f1e0c92b3f8
Parent:
0:4be393eec2a2
Child:
2:361360b2f1c5

File content as of revision 1:1f1e0c92b3f8:


/** fcopy: Copies a file
 *            Checks to insure destination file was created.
 *            Returns -1 = error; 0 = success
 */
int fcopy (const char *src, const char *dst) { 
    FILE *fpsrc = fopen(src, "r");
    FILE *fpdst = fopen(dst, "w");
    int ch = fgetc(fpsrc);
    while (ch != EOF) {
        fputc(ch, fpdst);  
        ch = fgetc(fpsrc);   
    }
    fclose(fpsrc);  
    fclose(fpdst);   
    int retval = 0;
    fpdst = fopen(dst, "r");
    if (fpdst == NULL) {
        retval = -1;
    } else {
        fclose(fpdst); 
        retval = 0;
    }
    return retval;
}

/** frename: renames a file (via copy & delete).
 *    Moves data instead of adjusting the file name in the
 *    file directory. Checks to insure the file was renamed.
 *    Returns -1 = error; 0 = success
 */
int frename(const char *oldfname, const char *newfname) {
    int retval = 0;
    if (fcopy(oldfname, newfname)) {
        remove(oldfname);    
    }     
    return retval;
}