How would I rename a folder using C++?
without using a system(); command, i need something like renameFolder() or something, thx :)
Any help would be appreciated,
thanks,
Matt N.
Printable View
How would I rename a folder using C++?
without using a system(); command, i need something like renameFolder() or something, thx :)
Any help would be appreciated,
thanks,
Matt N.
Code:#include <stdio.h>
#include <stdlib.h>
void main ()
{
char oldn[_MAX_PATH], newn[_MAX_PATH];
int result;
printf ("Enter the old filename: ");
gets (oldn);
printf ("Enter the new filename: ");
gets (newn);
result = rename (oldn, newn);
if (result == 0)
printf ("File successfully renamed.\n");
else
perror("Unable to rename file");
}
Depends on the operating system.
libraries like boost.filesystem provide portable access.
> gets (oldn);
Never use gets(). Read the FAQ for why.
>void main ()
main returns an int:
int main()
Here is the C program for MS-Windows os
Code:#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(int argc, char* argv[])
{
char oldn[_MAX_PATH], newn[_MAX_PATH];
int result;
printf ("Enter the old filename: ");
fgets (oldn,sizeof(oldn),stdin);
if(oldn[strlen(oldn)-1] == '\n')
oldn[strlen(oldn)-1] = 0;
printf ("Enter the new filename: ");
fgets (newn,sizeof(newn),stdin);
if(newn[strlen(newn)-1] == '\n')
newn[strlen(newn)-1] = 0;
result = MoveFile (oldn, newn);
if (result != 0)
printf ("File successfully renamed.\n");
else
{
char buf[255] = {0};
DWORD dwError = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);
printf("%s\n",buf);
}
return 0;
}