Menu

Damn strings and pointers!

2003-01-31
2012-09-26
  • Nobody/Anonymous

    Damn strings and pointers! I just can't get it right.

    Suppose this:

    MessageBox(NULL, "You have made this error x times", "Error", MB_OK);

    now I want to replace x with some counter:

    LPTSTR ModString(LPTSTR line)
    {
       counter ++;
       // chage line so x is replaced by counter
      return line;
    }

    and then I go:

    MessageBox(NULL, ModString("You have made this error x times"), "Error", MB_OK);

    ..and it doesn't work. It does work when I do this:

    LPTSTR ModString(LPTSTR line)
    {
       counter ++;
       char *temp = new char(strlen(line));
       // strcpy line into temp
       // chage temp so x is replaced by counter
       return temp;
    }

    But when I loop this function I run out of memory in no time, which makes sense because 'new' is never deleted.

    What should my ModString function look like for this to work?

    -Jay

     
    • Nobody/Anonymous

      hmmm... I don't have a link on me... but there is an excelent post by tkorrovi that does something very similar... i would highly suggest you search the forum for his name plus winmain and/example...

      Zero Valintine

       
    • upcase

      upcase - 2003-01-31

      This may be a stupid question since I don't do alot pure win api programming, but wouldn't wsprintf() fullfill your needs? Just a guess...

       
    • Patrick Ogay

      Patrick Ogay - 2003-01-31

      as I see, you just need the function in the messagebox.

      LPTSTR is, as I remember dependent of the definiton _UNICODE it's characterstring either char or wchar..
      if you work with char you never work with wide character anyway, so you could use:

      static string outfield   /* so you reuse your string */
      return outfield.c_str();

      I'm even not shure, whether the string must be static, as you just use it, after return.

      Patrick

       
    • Curtis Sutter

      Curtis Sutter - 2003-01-31

      MessageBox(NULL, ModString("You have made this error %d times"), "Error", MB_OK);

      LPTSTR ModString(char line[])
      {
        char temp[] = "You have made this error %d times";
        sprintf(line, line, counter++);
        return line;
      }

      Maybe somethiing like this would work better for you?  I am unfamilier with the LPTSTR type, so the return maybe buggy.  This can be fixed though I'm sure.

      Curtis

       
    • Nobody/Anonymous

      Patrick you are right. This is what I did:

      LPSTR ModString(string line)
      {
      counter ++;
      // chage line so x is replaced by counter
      LPSTR abc = (LPSTR)line.c_str();
      return abc;
      }

      this does not even depend on char or whar, because it is possible to do something like this:

      typedef std::basic_string<TCHAR> tString;

      and then use tString where it says string.

      My problem is now: WHY does this work (and the previous not)?

      -Jay

       
    • Nobody/Anonymous

      Thanks Curtis. At least I now know how that sprintf thing works. But, as you suspected, the return fails. It compiles ok, but when I run I get a memory allocation error.

      Zero Valentine, I have only found one tkorrovi post with the search criteria you mentioned and I can't see anything relevant there... thanks anyway.

      -Jay

       
    • Nobody/Anonymous

      Curtis,

      on second thought.. what's that char temp[] for?
      1. It isn't used and 2. it is identical to line

      -Jay

       
    • Curtis Sutter

      Curtis Sutter - 2003-02-01

      I was in a hurry, disregard that line.  Sorry, I should have dbl checked my post.

      Curtis

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.