Article View: alt.comp.lang.learn.c-c++
Article #482Re: String manipulation
From: Philip Staite
Date: Tue, 03 Oct 2000 00:00
Date: Tue, 03 Oct 2000 00:00
34 lines
2137 bytes
2137 bytes
News wrote:
> I need some help for string manipulation (without CString class).
> I know basics of C++, but never used strings.
> For example I need a function for appending a backslash after a directory
> name:
>
> VOID WINAPI AppendSlash(LPTSTR *lpFileName)
> {
> len = lstrlen(*lpFileName) + 1;
You realize that len will never be 0 now. Even when lpFileName is "" len
will be 1.
> if (len > 0 && szBuffer[len] != '\\')
The first test, len>0 will always be true. What is szBuffer? If it
duplicates what lpFileName is pointing at, then looking at [len] is probably
not what you want to do. Suppose lpFileName points to "foo.txt" then strlen
will return 7. Now, lpFileName[6] is 't' and lpFileName[7] is the
terminating NULL. You've got len set to point at in-effect lpFileName[8]
which is undefined.
> {
> // Modify string by adding a backsslash
> }
> }
If you know lpFileName points to memory that is long enough to accept the
additional character, why not just do something like:
int n = strlen(lpFileName);
if( n && ( lpFileName[n-1] != '\\' ) )
strcat( lpFileName, "\\" );
Technical Details
Message-ID:
<39DA07BE.E1B903E7@home.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<39da0385.0@news.ruca.ua.ac.be>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,f2706f20e1973f75 X-Google-Attributes: gid109fba,public X-Google-Thread: 103bde,f2706f20e1973f75 X-Google-Attributes: gid103bde,public X-Google-Thread: f5ca7,f2706f20e1973f75 X-Google-Attributes: gidf5ca7,public X-Google-Thread: 114592,f2706f20e1973f75 X-Google-Attributes: gid114592,public From: Philip Staite <pstaite@home.net> Subject: Re: String manipulation Date: 2000/10/03 Message-ID: <39DA07BE.E1B903E7@home.net> X-Deja-AN: 677091581 Content-Transfer-Encoding: 7bit References: <39da0385.0@news.ruca.ua.ac.be> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@home.net X-Trace: news1.rdc1.il.home.com 970590188 24.178.211.46 (Tue, 03 Oct 2000 09:23:08 PDT) Organization: J & D Computer World MIME-Version: 1.0 NNTP-Posting-Date: Tue, 03 Oct 2000 09:23:08 PDT Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.winapi,borland.public.cppbuilder.winapi,comp.lang.c++