Question about renaming a File

This is a discussion on Question about renaming a File within the C++ Programming forums, part of the General Programming Boards category; Is there a command that I could rename (or create) a txt file having the name of the user's input? ...

  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
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    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, 11: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-22-2008, 12:19 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

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