delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/09/24/11:00:29

Message-ID: <39CE1EF7.12DAA29F@user.rose.com>
From: April <awhite AT user DOT rose DOT com>
X-Mailer: Mozilla 4.7 [en]C-CCK-MCD {TLC;RETAIL} (Win95; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: GPF caused by string functions...
References: <8qi4hm$ism$1 AT nnrp1 DOT deja DOT com>
Lines: 42
Date: Sun, 24 Sep 2000 10:34:15 -0500
NNTP-Posting-Host: 205.189.215.3
X-Trace: client 969805643 205.189.215.3 (Sun, 24 Sep 2000 10:27:23 EDT)
NNTP-Posting-Date: Sun, 24 Sep 2000 10:27:23 EDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

raynee AT my-deja DOT com wrote:

> I am trying to get some string functions to work as part of the
> interface for my program. I am using ScreenPutString to write to the
> screen and my code appears to work when I call my centrestring()
> function once it works okay. A second call, however, causes a General
> Protection fault no matter how I specify my parameters. The code that I
> am using is:

It isn't really the second call that is failing - comment out either and the
crash occurs.

Did you compile with -g and use symify [symify app.exe].  The crash is on
the line
    buf[width] = 0;
but it actually misleading.

I believe the problem is that the buf[] is not set to '\0', so
strcat(buf,str)
cannot find the end of the string.

    i = strlen(str);                  // determine length
    j = (width - i) / 2;              // determine starting point
    memset(buf,' ',--j);              // clear the start of the buffer
    strcat(buf,str);                  // append the string to the buffer
    memset(spacer,' ',(width-i-j));   // clear the end of the space buffer
    strcat(buf,spacer);               // append spaces
    buf[width] = 0;                   // terminate string buffer
    putstring(buf,fg,bg,col,row);     // call putstring()

If your program uses any sort of stdio, you may want to re-write the above
to
use sprintf().  The "*.*s" format specifier should let you get the above.

The is not tested, but the concept is good:

    i = strlen(str);                  // determine length
    j = (width - i) / 2;              // determine starting point
    sprintf( buf, "%*s%*.*s", j, "", -(width-j), width-j, str);

good luck

- Raw text -


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