Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

renaming a file by c++

Status
Not open for further replies.

ehsanelahimirza

Full Member level 6
Joined
Feb 24, 2006
Messages
334
Helped
28
Reputation
56
Reaction score
7
Trophy points
1,298
Activity points
3,570
hi

plz tell me how to rename a file by using c++

i want to take path by user and want to take just new name without path
 

It can be done like this. Anyway this is not a good method.

#include "stdio.h"
#include "dos.h"
int main()
{
system("ren 6.txt 6666.txt");
}

This program will rename the 6.txt as 6666.txt. It is done with TurboC in windows.
The restiction here is that the file that is to be renamed must be in the TC folder.

If anyother method is there please let us know
 

Also you can use the function rename( )

here is an example:

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char old_name[20], new_name[20];
    
    cout<<"Enter filename: ";
    cin>>old_name;
    cout<<"Enter new file name: ";
    cin>>new_name;
    
    if(rename(old_name, new_name)==0)
    cout<<"Success\n";
    else
    cout<<"error";
    
    system("PAUSE");

    return EXIT_SUCCESS;
}
 

hi
in vc++ u can use from

BOOL MoveFile(
LPCTSTR lpExistingFileName, // file name
LPCTSTR lpNewFileName // new file name
);

in any where u want!
 

Hi saeid5977,
MoveFile is a win32 api and will work only on Windows OS and if we need to make it generic on different platforms,it better to stick with c++ Ansi runtime library calls.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top