tags:

views:

77

answers:

6

I am trying to iterate through char*

Is there any way to like reset these char* strings back to blank?

I am trying to reset from1 and send1.

Is there anything else wrong with my code.. it is only copying the first file in my array

 for(i = 0; i < 3; i++)
 {
     from1 = " ";
     send1 = " ";
     from1 = strncat(fileLocation,filesToExport[i],50);
     send1 = strncat(whereAmI,filesToExport[i],50);

     CopyFile(from1,send1,TRUE);
     printf("%s\n",from1);
     printf("%s",send1);
 }
+1  A: 

The easiest way is to set the first byte to 0. Like this:

from1[0] = 0;
send1[0] = 0;

C/C++ checks the end of a char* string by looking for the 0 byte. It doesn't care what follows that.

Vilx-
A: 

you mean like

send1[0] = 0;
from1[0] = 0;

?

Thomas
+3  A: 

THe strings are nul terminated, which means they have a zero character at the end. You can set the first char in the string to zero to truncate it back to being empty:

from1[0] = '\0';

Another way would be to copy a blank string:

strcpy(from1, "");
Jason Williams
+1 for using '\0'.
Steven Sudit
I totally agree with Steven. char is the correct type here. Thanks for using the correct literal. :D
Zachary Murray
This will put a null at the beginning of filelocation, hence next time round the loop its wrong! Either put teh null at the end the original filelocation or use a separate buffer.
djna
It won't be wrong, it'll be empty.
Steven Sudit
file location appears to be the directory part of the destination, we just zapped it. C:/dir/ was strncatted to C:/dir/theFileToCopyNext time round the loop we wat to put the null at position 7, over the t of theFileToCopy not at 0 over the C of C:
djna
+1  A: 

To clear a string to empty, so that strncat() has an empty string to concatenate to, just do:

from1[0] =  '\0';

This sets the first character to the zero terminator that indicates end of string, thus making the string have length 0. This assumes that from1 is an actual modifiable char buffer, but your call to strncat() implies that it is.

unwind
+2  A: 

What do you mean by "blank"? Zeroed, spaces, or empty?

For filling a memory area you're best off using memset(), so

#include <string.h>

memset(pBuffer, ' ', length); /* Fill with spaces */
pBuffer[length] = '\0'; /* Remember to null-terminate manually when using memset */ 

memset(pBuffer, '\0', length); /* Fill with zeroes */

pBuffer[0] = '\0'; /* Set first element to null -- effectively set the string 
                    * to length 0
                    */
Christoffer
+1  A: 

You are copying into filelocation and whereami. Are they buffers or strings? You may be writing off the end of your string.

I think you would do better to allocate a suitably sized buffer

fromLen = strlen(fileLocation);
fileLen = strlen(filesToExport[i]);

from1 = malloc(fromLen + fileLen + 1);

/* add check here that string fits */

strcpy( from1, filelocation);
strcat( from1 + fromLen, filesToExport[i]);

/** etc **/

free(from1);
djna