www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/06/18/11:59:18

Date: Mon, 18 Jun 2001 18:55:57 +0300
From: "Eli Zaretskii" <eliz AT is DOT elta DOT co DOT il>
Sender: halo1 AT zahav DOT net DOT il
To: "Graham Reeds" <grahamr AT ntlworld DOT com>
Message-Id: <7458-Mon18Jun2001185556+0300-eliz@is.elta.co.il>
X-Mailer: Emacs 20.6 (via feedmail 8.3.emacs20_6 I) and Blat ver 1.8.9
CC: djgpp AT delorie DOT com
In-reply-to: <u0pX6.32176$9t5.2935940@news6-win.server.ntlworld.com>
(grahamr AT ntlworld DOT com)
Subject: Re: Extracting a directory name.
References: <u0pX6.32176$9t5 DOT 2935940 AT news6-win DOT server DOT ntlworld DOT com>
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> 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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019