Hi-

Trying to rename a file using the rename() function. It works, but it appends an '@' to the end of the renamed file.

Here's my code:

char oldname[] ="file1.csv";
char newname[26];

  newname[0] = '3';
  newname[1] = '1';
  newname[2] = '1';
  newname[3] = '3';
  newname[4] = '3';
  newname[5] = 'I';
  newname[6] = 'n';
  newname[7] = 'v';
  newname[8] = 'e';
  newname[9] = 'n';
  newname[10] = '2'; 
  newname[11] = '0';    
  newname[12] = date[6];
  newname[13] = date[7];
  newname[14] = date[0];
  newname[15] = date[1];
  newname[16] = date[3];
  newname[17] = date[4];
  newname[18] = time[0];
  newname[19] = time[1];
  newname[20] = time[3];
  newname[21] = time[4];
  newname[22] = '.';
  newname[23] = 'c';
  newname[24] = 's';
  newname[25] = 'v';

  result= rename( oldname , newname );

This works, except it appends the @ at the end of the new file. Any ideas on how to remove that? (The date and time arrays are defined earlier in the program)

I don't see where you are inserting a '\0' (NULL char) to terminate the C-string. The '@' must be an extraneous character stored in a byte adjacent to the memory that holds the C-String.

Edited 6 Years Ago by Fbody: n/a

It's actually C++, I thought that only C required a terminator.

The funny thing is, it works as desired on some machines, but not all.

How about using the dos command rename? Consider this pseudocode (yes, I love pseudocode):

main function{
    print("enter old path")
    input>>old_path
    print("enter new path")
    input>>new_path
    system("pause")
    system("rename ", old_path, new_path)
    print("complete")
    }

I would give you a proper code, but I'm working back into console based C++, after spending 2 years in python. I want to learn more C++ than I originally did. Here's a couple of links:

http://www.computerhope.com/renamehl.htm
http://www.computing.net/answers/programming/dos-commands-in-c/9449.html
(on the second link, the second post should give you an idea)

Hope this helps :)

Well, adding the terminator fixed it. Thank you for your help ;)

It's actually C++, I thought that only C required a terminator.

The funny thing is, it works as desired on some machines, but not all.

C++ is a "superset" of C. As such, and for compatibility reasons, the functions and constructs from C work the same, at the language's abstraction level, in C++ as they do/did in C.

A C-style string is still a null-terminated array of char, C++ does not change that behavior.

C++ has a string class that is designed to allow you to treat it like a native data type, but it is nothing like a C-Style string. The C++ string class is NOT simply a null-terminated array, it is much more than that. That being said, it does have the ability to make a C-Style string of its contents available to client code.

Edited 6 Years Ago by Fbody: n/a

This question has already been answered. Start a new discussion instead.