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.
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.
Last edited by guitarist809; 08-16-2006 at 09:30 AM.
~guitarist809~
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.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
> 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; }