File rename script (char <--> string)

This is a discussion on File rename script (char <--> string) within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm trying create basic script for renaming files with date included. If I rename only char oldname to newname ...

  1. #1
    Registered User kaspari22's Avatar
    Join Date
    Jul 2008
    Location
    Czech Republic, Doubravice
    Posts
    14

    File rename script (char <--> string)

    Hello,

    I'm trying create basic script for renaming files with date included. If I rename only char oldname to newname without date included it's working but when I include date it seems like it's string after and renamed can be only char... Even I tried restream string back to char I have a problems as

    initializer fails to determine size of `newname1' because of "#define MAX_DATE 12"

    I'm trying find way how basically rename files from original to new with current date included...

    Any idea?

    Example:

    ----------------------------------------

    Code:
    #include <sstream>
    #include <time.h>
    
    #define MAX_DATE 12
    
    
    
       time_t now;
       char the_date[MAX_DATE];
       the_date[0] = '\0';
    
       now = time(NULL);
    
       if (now != -1)
       {
          strftime(the_date, MAX_DATE, "%d_%m_%Y", gmtime(&now));
       }
       string datex = the_date;
       string file1 = "filename" + datex + ".TXT";
       stringstream ss;
       char replace1[] = "";
       ss << file1;
       ss >> replace1;
    
    
       int result1;
      char oldname1[] = "FILENAME.TXT";
      char newname1[] = replace1;
      result1= rename( oldname1 , newname1 );
      if ( result1 == 0 )
        puts ( "File successfully renamed" );
      else
        perror( "Error renaming file " );
    -----------------------------------------------------------

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > char replace1[] = "";
    You haven't allocated enough space (just one character in fact).

    > result1= rename( oldname1 , newname1 );
    Rather than messing around with char arrays, do everything with std::string variables.

    When it comes to the rename, do this
    result1= rename( oldname.c_str() , newname.c_str() );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User kaspari22's Avatar
    Join Date
    Jul 2008
    Location
    Czech Republic, Doubravice
    Posts
    14

    re:

    Wao,

    thank you very much,
    it's working...
    also I insert char replacement: string datex = the_date;
    "datex" is char

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21