Thread: rename a file on the C drive

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    13

    Question rename a file on the C drive

    Hello programmers,
    Can anyone tell me how to rename a file using C?

    Ex-
    I have a file c:\My Documents\Test File\Test\testLog.log and I would like to have a C program that will run and test to see if the testLog.log file is there first then if so rename it. After it has been renamed the file testLog.log shouldn't be there any more.

    Before:
    c:\My Documents\Test File\Test\testLog.log

    After:
    c:\My Documents\Test File\Test\testLog1.log


    Do I have to open two files? One being my file and the other being the one I want to rename it to? Then copy all of the info from file1 to file2? Or is there an easier way?

    Thanks for your help,
    Dan
    Thanks,
    Cyberpuck52

    "If you know the enemy and know yourself, you need not fear the result of a hundred battles"
    -Sun Tzu, Art of War

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    26
    Try the function rename, it's stdio.h
    Code:
    if(rename(oldname, newname ) != 0)
        printf("Rename Error");
    you don't need to open files.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    13

    Red face

    Can it be that simple? I will give it a try, Thanks!!!
    Thanks,
    Cyberpuck52

    "If you know the enemy and know yourself, you need not fear the result of a hundred battles"
    -Sun Tzu, Art of War

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    13

    Talking

    Edge,
    Thanks man that works great!!

    #include <stdio.h>

    int main(void)
    {
    char oldname[80], newname[80];

    /* prompt for file to rename and new name */
    printf("File to rename: ");
    gets(oldname);
    printf("New name: ");
    gets(newname);

    /* Rename the file */
    if (rename(oldname, newname) == 0)
    printf("Renamed %s to %s.\n", oldname, newname);
    else
    perror("rename");

    return 0;
    }

    Thanks,
    Cyberpuck52

    "If you know the enemy and know yourself, you need not fear the result of a hundred battles"
    -Sun Tzu, Art of War

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM