I'm trying to write a program that will simply move a file from "FILENAME.TXT" to "CURRENTDATE-CURRENTTIME-FILENAME.txt" and am having problems.


The error I'm getting is cannot convert from char to char[]

Here is my simple code:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

  			   time_t now;
			   char the_date[12];
			   the_date[0] = '\0';
			   now = time(NULL);
			   strftime(the_date, 12, "%d_%m_%Y", gmtime(&now));
				char message[] = the_date[12];
			
			   char oldname[] ="C:\\MilSequence.avi";
			   //char newname[] = &the_date;
			   //rename( oldname , newname );
			   
			
			 }

It helps if you point to the offending line.

I'm supposing it's this:

char message[] = the_date[12];

What you're attempting here is to allocate an array of char and you want it to be initialized to the contents of the date string.

This fails on two counts.
First, the_date[12] is a single character (actually, the character that "would" occur right after the ones you allocated - you've gone past the end of the array).

Secondly, to initialize a char array, you must provide an array list. You cannot assign another array in the way you're attempting. So, it will take two steps.

char message[12] = "";  //initialize to empty string, just to be tidy
strcpy( message, the_date );

It's absolutely senseless statement in C++:

char message[] = the_date[12];

You try to declare an array of unknown size, so you must provide brace-enclosed initializer-list with constant initializers or string literal (see declaration of oldname), but the_date[12] in this context denotes non-existing 13-th element of the_date array (it's char element). You can't initialize an array of unknown size with another array name.
I don't know why you need message char array if you have the_date char array with current date contents. If you want to copy the_date C-string to another char array, declare that array with defined size then use strcpy function, for example:

char message[12];
stcpy(message,the_date); // it's interesting what for...

Thanks guys, I have that working now.

Now, last question. How would I add "C:\\" to the front of the_date.

//copy the time to string the_date
  			   time_t now;
			   char the_date[12];
			   the_date[0] = '\0';
			   now = time(NULL);
			   strftime(the_date, 12, "%d_%m_%Y", gmtime(&now));
			   
			   //I need to make the_date into c:\\DATE.avi

			   //make a new name for the file.
			   char newname[] = "";
			   strcpy (newname, the_date );
			   char oldname[] ="C:\\newname.avi";
			   rename( oldname , newname );

I noticed the string .insert() function, however it appears not to be working because the_date is a character array?

Sorry, Vmanes, but I did not see your post!..

>char newname[] = "";
Good luck with that. Keep in mind that the absolute size of newname is 1, and be sure not to overflow the array. Don't forget to terminate your string with '\0'. ;)

>strcpy (newname, the_date );
Oops, $10 says you overflow the array 100% of the time.

>How would I add "C:\\" to the front of the_date.
I don't know how you would do it, but I would do it like this:

std::string newname = "C:\\";

newname += the_date;

rename ( "C:\\newname.avi", newname.c_str() );

Well here's what I have that's currently non-working. Hopefully I can get some insight.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 //declarations
				 char newname[] = "c:\\ahhhh.avi";
			 char oldname[] ="C:\\newname.avi";
			 time_t now;
			 char the_date[12];
			 
			 //copy the time to string the_date
  			 the_date[0] = '\0';
			 now = time(NULL);
			 strftime(the_date, 12, "%d_%m_%Y", gmtime(&now));
			   
			 //I need to make the_date into c:\\DATE.avi
			 std::string newnameobject("");
			 newnameobject = "C:\\";
			 newnameobject.append(the_date);
			 newnameobject.append(".avi");

			 strcpy(newname, newnameobject.c_str());

			 //make a new name for the file.

			 rename( oldname , newname );
			   
			
			 }

Narue:

>strcpy (newname, the_date );
Oops, $10 says you overflow the array 100% of the time.

If you're referring to my example above, pay up. Destination was declared same size as source, so if the source was safely filled (11 char plus NULL), the strcpy will not overflow.

In the code given, the source array gets a 10 character string.

Within the context of OP's code, the drive prefix could be added by:

char filename[128] = "";
strcpy ( filename, "C:\\" );
strcat ( filename, the_date );
//or safer as
strncat (filename, the_date, 123 );

ArkM - no problem, you were probably writing at the same time I was.

stormtr00per - in your latest version, you better be sure newname[] is given a size big enough to hold whatever you'll store there. In dealing with char array based strings, I find it best to allocate overly large. Only use the initializer based sizing when you're making a string constant.

const char page_label[] = "Page number: ";
char some_string[256] = "";

>char newname[] = "";
Good luck with that. Keep in mind that the absolute size of newname is 1, and be sure not to overflow the array. Don't forget to terminate your string with '\0'. ;)

>strcpy (newname, the_date );
Oops, $10 says you overflow the array 100% of the time.

>How would I add "C:\\" to the front of the_date.
I don't know how you would do it, but I would do it like this:

std::string newname = "C:\\";

newname += the_date;

rename ( "C:\\newname.avi", newname.c_str() );

Thanks for helping me out. It is working like a charm now. I appreciate the help.

For anyone who googles this in the future here is the final code:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 //declarations
			 
			 
			 time_t now;
			 char the_date[12];
			 
			 //copy the time to string the_date
  			 the_date[0] = '\0';
			 now = time(NULL);
			 strftime(the_date, 12, "%d_%m_%Y", gmtime(&now));
			   
			 //I need to make the_date into c:\\DATE.avi
			 std::string newnameobject("");
			 newnameobject = "C:\\";
			 newnameobject.append(the_date);
			 newnameobject.append(".avi");

			 //make a new name for the file.

			 rename( "C:\\newname.avi" , newnameobject.c_str() );
			   
			
			 }

>How would I add "C:\\" to the front of the_date. strftime(the_date, 12, "c:\\%d_%m_%Y", gmtime(&now));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.