Mail Archives: djgpp/2001/06/18/11:59:18
> From: "Graham Reeds" <grahamr AT ntlworld DOT com>
> Newsgroups: comp.os.msdos.djgpp
> Date: Mon, 18 Jun 2001 16:22:10 +0100
>
> I have a piece of code which isn't working, and I can't think for the life
> of me why. I have isolated the fault down to these lines with my
> error-checking (printf()s) still in:
>
> Ptr = strrchr(Directory, '\\');
> bytestocopy = strlen(Directory) - strlen(Ptr);
> printf("%i\n", bytestocopy);
> strcpy(FileName, Ptr+1);
> strncpy(BaseDir, Directory, bytestocopy);
> printf("BaseDir: %s (%li)\n", BaseDir, strlen(BaseDir));
Did you consider the possibility that strncpy might not null-terminate
the resulting string in BaseDir?
(Btw, there are library functions to do this, called `basename' and
`direname'. They are better than your code, because they also support
forward slashes and file names like d:foo.)
> However I also
> need the info before the slash, and this is the piece that isn't working.
> The amount to copy is strlen(Directory) - strlen(Ptr) but for some reason
> always appends 2 extra bytes to the end or assorted gibberish
Probably because it's not null-terminated.
- Raw text -