Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Changing a dir's name (Should be simple)

This topic is 6011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here''s a little piece of code that has simply the function to rename a directory (From "OriginalName" to "OriginalName2"). BUT the code does NOTHING ! I''ve created the proper directory and entered the right name, but nothing changes. What could be the reason for this ??? Here''s the code:
	char buffer[32];

	printf("Enter the directory''s name: ");
	gets( buffer );

	oldname = buffer;
	newname = buffer;
	strcat(newname, "2");

	rename( oldname, newname );
thanks, Vk

Share this post

Link to post
Share on other sites
That code makes a single buffer of 32 characters. It gets a string in it. It then sets two pointers equal to that arrays address. Then it appends two to that address. So, if you enter "fish" as the file you're calling "rename("fish2","fish2")" (yes, it edits both strings). Try printing out newname and oldname as text, you'll see. Also, I don't know if rename does directories, but I'd think it would (but it isn't required since the ANSI standard says nothing about directories).

Edited by - Null and Void on January 31, 2002 8:17:23 AM

Share this post

Link to post
Share on other sites
Of course it does nothing. You''re making mistakes with your pointers to array.

You can not just assign a pointer to an array of chars, and think you suddenly have 2 strings. It doesn''t work like that. The pointer ends up pointing to the same string.

Effectively, you''re calling rename ("dir2", "dir2").

You need to make another character array for the new name, use strcpy to duplicate the original, then use strcat to append the "2".

Share this post

Link to post
Share on other sites
Looks to me that your code ends up calling

rename( ''ogname2'', ''ogname2'');

and since the ''ogname2'' directory doesn''t yet exist - nothing happens.

Try using a different buffer for newname, then copy the oldname into it. Then append "2". Then call rename.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!