   problems copying a part of a string
  hi, I have the following problem :when I try to copy a few characters from 1 string to another I get that few characters, but the rest of the string is garbage.Although I emptied the string first[\code]pdb is a pointer to a structure that contains a string DirectoryBestand = ".\\database.idx" & StnTabel = "database.idx"strcpy(pdb->Directory, "") ;strncpy (pdb->Directory, Bestand, (strlen(Bestand)) - (strlen(StdTabel))) ; // where strlen(bestand) is 14 and strlen(StdTabel) is 12 (checked this with a printf)result : .\¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨d¨das you can see I try to seperate the directory from a string that contains the directory + filehope some1 can help methanx in advance   
  Looks like string corruption to me.  I checked the MSDN library reference (because I was wondering m'self) and it states that strncpy doesn't check for buffer overruns.  It looks like you're copying that string of length n to a string of length 1 (just your '\0' character).Also, keep in mind that things that don't check for buffer overruns usually don't append the terminating character ('\0') in a case where strlen(strDest) = n and strlen(strSource) = n + 1.  I can't think of how to write that out, but hopefully you understand.  If not, just say so and hopefully someone else can help  [edited by - ShadowWolf on February 15, 2004 7:28:48 PM];  First: you didn''t post the declaration/initialization of Directory. Is this a char array? Malloc-ed pointer? Garbage value? How big?Second: strncpy() does not 0-terminate the string; you have to do that manually. ;  I fixed it this waystrncpy (Bestand, Bestand, (strlen(Bestand)) - (strlen(StdTabel))) ;strcpy(pdb->Directory, Bestand) ;So, now I replace Bestand with the short version of Bestand (only directory) and after that I copy Bestand over pdb->Directoryso it must have something to do with my char Directory[180] inside my structure (char Bestand[180] is outside that structure)although I still don't understand why the first method didn't work   [edited by - da cobra on February 16, 2004 6:41:02 AM];quote:Original post by Da cobra   although I still don't understand why the first method didn't work   Because (as both ShadowWolf and hplus0603 have already stated) strncpy DOES NOT terminate the string   (if the length of the source is more than the value of the 3rd parameter). RTFM!Also, you are now inviting trouble by making the source and the destinations the same. This is also clearly stated in my docs for strncpy. RTFM! [edited by - JohnBolton on February 16, 2004 1:43:59 PM]