Thread: Rename Error

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    Rename Error

    Hello,

    I am currently Trying to rename a file to a randomly created string. Every time a file is passed to my program i want it to be renamed to a very random name always ending in '.dl'. However i am experience some difficulty's. When i pass a file to the program the file disappears totally.

    Thank You For Any Help.

    Here is the program:

    Code:
    #include <iostream>
    #include <windows.h>
    
    #include <cstdio>
    
    char Random_Char;
    char Altered_Filename[16];
    
    using namespace std;
    
    void Get_Random_Char()
    {	
    	Random_Char = (rand()%62);
    
    	if(Random_Char <= 25) 
    	{	
    	   Random_Char += 97;	
    	}
    	else if(Random_Char >= 26 && Random_Char <= 51) 
    	{   
    	   Random_Char += 39;
    	}
    	else if(Random_Char >= 52) 
    	{		 
    	   Random_Char -= 4;	
    	}
    }
    
    void Rename_The_File()
    {
    	 int Chars_In_Name = 0;
    	 
    	 srand(time(0));
    	 
    	 do{
    		 
    	 Get_Random_Char();
    
    	 Altered_Filename[Chars_In_Name] = Random_Char;
    		
    	 Chars_In_Name++;
    
    	 }while (Chars_In_Name != 13);
    	
    	 Altered_Filename[13] = '.';
    	 Altered_Filename[14] = 'd';
    	 Altered_Filename[15] = 'l';
    	 Altered_Filename[16] = '\0';
    }
    
    int main(int argc, char *argv[])
    {
    	Rename_The_File();
    	
    	cout << argv[1] <<"\n\n";
    	cout << Altered_Filename <<"\n\n";
    	
    	system("pause");
    	
    	if (rename(argv[1], Altered_Filename) != 0)
    	{
    	   cout << "ERROR!!!\n\n";
    	}
    	else
    	{
    	   cout << "Sucess!!!\n\n";
    	}
    	
    	system("pause");
    }
    The output is:
    Code:
    C:\Documents and Settings\Chris\Desktop\New Text Document.txt
    
    hYtMPRRtUNAHA.dl
    
    Press any key to continue . . .
    Sucess!!!
    
    Press any key to continue . . .

    (New Text Document.txt Is no longer on desktop nor is there a file called hYtMPRRtUNAHA.dl)

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    char Random_Char;
    char Altered_Filename[16];
    Avoid global variables - use arguments.

    Code:
    	if(Random_Char <= 25) 
    	{	
    	   Random_Char += 97;	
    	}
    	else if(Random_Char >= 26 && Random_Char <= 51) 
    	{   
    	   Random_Char += 39;
    	}
    	else if(Random_Char >= 52) 
    	{		 
    	   Random_Char -= 4;	
    	}
    Confusing. Try using chars instead - if Random_Char <= 'a') for example.

    Code:
    	 do{
    		 
    	 Get_Random_Char();
    
    	 Altered_Filename[Chars_In_Name] = Random_Char;
    		
    	 Chars_In_Name++;
    
    	 }while (Chars_In_Name != 13);
    Indent properly.

    Code:
    	 Altered_Filename[13] = '.';
    	 Altered_Filename[14] = 'd';
    	 Altered_Filename[15] = 'l';
    	 Altered_Filename[16] = '\0';
    Preferably, use std::string or strcat.

    Code:
    	if (rename(argv[1], Altered_Filename) != 0)
    As you can see, the argument is the full path to the file, but your renamed filename isn't. The behavior is actually defined, but you're overlooking it.
    See rename for more information. Specify full path or switch current directory to the directory where your file to be renamed is.

    Try not to mix spaces and tabs. Use only tabs or only spaces for indentation. I recommend tabs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't believe you that the file is being deleted, assuming it even existed to begin with. What do you actually think that code is doing?

    And furthermore, why do you think that?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think Elysia is right, you should be preserving the path (eg. "C:\Documents and Settings\Chris\Desktop\") and just append a random name to the path.

    You should probably also check if the file exists before trying to rename it. Sure it's extremely unlikely to exist, but it's those long shots that create the hard to find bugs.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    To preface this, I'm not really a C++ programmer. I prefer the C approach, but here C++ strings should be used for simplicity.

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    void getRandomName(std::string &szRename);
    
    void getRandomName(std::string &szRename)
    {
    	char *szAlphabet = "abcdefghijklmnopqrstuvwxyz";
    
    	szRename.erase();
    	for(int i=0;i<20;i++)
    	{
    		szRename += szAlphabet[rand() &#37; (strlen(szAlphabet)-1)];
    	}
    	szRename += ".dl";
    }
    
    int main(int argc, char *argv[])
    {
    	std::string szPath, szRename, szFull;
    	size_t i;
    	
    	srand(time(NULL));
    	
    	if(argc < 2)
    	{
    		std::cout << "Usage: " << argv[0] << " <file>" << std::endl;
    		return 1;
    	}
    	szPath = argv[1];
    	getRandomName(szRename);
    	i = szPath.find_last_of('\\');	// Note: \\ is Windows based.
    	if(i != std::string::npos)
    	{
    		szFull = szPath.substr(0, i+1);
    		szFull = szFull + szRename;
    	}
    	else szFull = szRename;
    	std::cout << "szFull = " << szFull << std::endl;
    	if(rename(argv[1], szFull.c_str()) != 0)
    	{
    		std::cout << "Error:  Unable to rename file." << std::endl;
    		return 2;
    	}
    	std::cout << "Renamed from \"" << szPath << "\" to \"" << szFull << "\"." << std::endl;
    	return 0;
    }
    If you notice, most of the code is error checking. It handles strings that have backslashes and those that don't. It doesn't currently handle forward slashes, however, but since the OP included Windows.h, I had assumed a Windows platform.

    Apologies for any errors. This hasn't really been checked.

    Also, apologies for my earlier post. I didn't read the code thoroughly enough.
    Last edited by MacGyver; 12-16-2007 at 09:18 PM.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    3
    Thank you very much to all

    Thanks MacGyver
    That code works now i just have to learn what it all is!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM