Thread: Question about renaming a File

  1. #1
    Unregistered
    Guest

    Question about renaming a File

    Is there a command that I could rename (or create) a txt file
    having the name of the user's input?

    For example...

    If the user inputs "FILENAME", I want the program to be able to

    either create a file named "FILENAME.txt"

    or to
    rename a pre-existing file to "FILENAME.txt"

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    in stdio.h there is a rename()

    syntax is

    rename(oldname,newname);

  3. #3
    Unregistered
    Guest

    here is my coding.

    I still could not work it out.
    Here is the coding.

    cout<<"Enter the name of file"
    cin>>newname;

    FILE *create;
    create = fopen ("oldname.txt","w+");

    fseek (create, SEEK_SET, 2);

    fclose (create);

    rename("oldname.txt",newname);

    But the filename remains to be "oldname.txt".
    What am I doing wrong?

  4. #4
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    i created a file renaming program using turboc++ ver 3
    it was something like this

    ----
    char new[12],old[12];

    cout<<"enter old name ";
    gets(old);
    cout<<"enter new name ";
    gets(new);

    rename(old,new);
    ---


    it worked except that the file name should be in dos format.

    it could be because you haven't defined the size for newname
    try that

  5. #5
    Ya that code is correct, I made one myself, the only difference is I used cin>> instead of gets();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM