Date: Mon, 18 Jun 2001 18:55:57 +0300 From: "Eli Zaretskii" Sender: halo1 AT zahav DOT net DOT il To: "Graham Reeds" 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: (grahamr AT ntlworld DOT com) Subject: Re: Extracting a directory name. References: 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 Precedence: bulk > From: "Graham Reeds" > 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.