StrBuf::Terminate()

Null-terminate the string pointed to by the buffer member of a StrBuf. The null byte is placed in the buffer at the location indicated by the length member.

Virtual?

No

 

Class

StrBuf

 

Arguments

None

 

Returns

void

 

Notes

Initialize the StrBuf before calling Terminate().

The length member of the StrBuf is effectively unchanged by Terminate().

Example

Terminate() is defined in strbuf.h as follows:

void Terminate()
{
    Extend( 0 ); --length;
}

Terminate() null-terminates the string by calling Extend( 0 ), which also increments the length member; the length is then decremented within Terminate(), leaving it unchanged.

See also

StrBuf::StringInit()

Example

#include <iostream>

#include <stdhdrs.h>
#include <strbuf.h>

int main( int argc, char **argv )
{
    StrBuf sb;

    sb.Set( "xyzzy" );

    cout << "Prior to sb.SetLength( 3 ) and sb.Terminate():\n";
    cout << "  sb.Length() returns " << sb.Length() << "\n";
    cout << "  sb.Text() returns \"" << sb.Text() << "\"\n\n";

    sb.SetLength( 3 );

    cout << "After sb.SetLength( 3 ) but prior to sb.Terminate():\n";
    cout << "  sb.Length() returns " << sb.Length() << "\n";
    cout << "  sb.Text() returns \"" << sb.Text() << "\"\n\n";

    sb.Terminate();      // null-terminate the string at length

    cout << "After sb.SetLength( 3 ) and sb.Terminate():\n";
    cout << "  sb.Length() returns " << sb.Length() << "\n";
    cout << "  sb.Text() returns \"" << sb.Text() << "\"\n";
}

Executing the preceding code produces the following output:

Prior to sb.SetLength( 3 ) and sb.Terminate():
  sb.Length() returns 5
  sb.Text() returns "xyzzy"

After sb.SetLength( 3 ) but prior to sb.Terminate():
  sb.Length() returns 3
  sb.Text() returns "xyzzy"

After sb.SetLength( 3 ) and sb.Terminate():
  sb.Length() returns 3
  sb.Text() returns "xyz"