Welcome to RenEvo Sign in | Join | Help
in
Home IRC Wiisis C&C FarCry Blogs Forums Photos Downloads

Question about #define (C++)

Last post 12-10-2006, 3:54 PM by Jary. 9 replies.
Sort Posts: Previous Next
  •  12-08-2006, 1:16 AM 316

    Question about #define (C++)

    Hello,

     First of all, I know this board is for VB.Net, but I know most of you knows C++ as well, so I'm wondering if it could be for C++ too, or if they could be a C++ board ? If you make one Dante, you can be sure i'll spam it with question !

     So here is my question, if it is not allowed, then i'm sorry and just delete my post, otherwise:

     

    I'm making a IRC bot in C++. I have a problem with #define.

    I used #define this way:

    #define join "JOIN %s\n"

    My problem is to edit %s before seind it with send().

    I tried:

    char *bsend =(char *)malloc(sizeof(char)*50);

    char *chan = "#channelname";

    sprintf(bsend,join,chan);

     

    But the compiler tells me that "sprintf was disaproved". I read somewhere, that in C++ you should not use sprintf, but they didn't explain why. Would anyone be able to give me a hand on this please ? I thank you in advance.

  •  12-08-2006, 2:02 AM 317 in reply to 316

    Re: Question about #define (C++)

    Sorry to post so fast, but I have a second question:

    char *p;

    p = strstr((char *)buf_str, "PING :");

    string r = p;

    r = r.substr(6);

    r = "PONG :" + r;

    r.copy(p,r.size());

     

     

    I get an advertise from VC++, that my construction was disaproved for "copy".

    Message : 'You have used a std:: construct that is not safe. See documentation on how to use the Safe Standard C++ Library'

     

    Could anyone help me here too please ? :)

    Thanks

  •  12-08-2006, 8:37 AM 318 in reply to 317

    Re: Question about #define (C++)

    In response to your first question: I assume you are using Visual C++ 2005? If so, VS 2005 doesn't like you using sprintf, because it has a risk of a buffer overflow. Microsoft provide their own alternative to this called sprintf_s which is declared as the following:

     int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format [, argument] ...);

    Here is an example:

    sprintf_s(buffer, 200, "%s has joined", personName);

    So sprintf_s will only write up to a maximum of 200 characters into buffer. 

     

    I'm not so sure about your second question though. I think that (apparently) VS has a problem with the normal STL. You could look on the MSDN about the "Safe Standard C++ Library" and use that to make the problem go away.

  •  12-08-2006, 11:18 AM 319 in reply to 318

    Re: Question about #define (C++)

    r = "PONG :" + r;

    This isn't the correct way to do what you want to do.

    r = std::string("PONG :") + r;

     Use the constructor to turn the c-style string into an STD string object and use its overloaded addition operator to concatenate the strings.

     

    Dan is correct on the first question.  MSVS 2005 has deprecated many stdio functions.  Either conform to MS standards or get a true ANSI-C compliant compiler.  This is why I don't use MSVS 2005.  MS can kiss my ass with their own set of rules.

  •  12-09-2006, 8:40 AM 324 in reply to 319

    Re: Question about #define (C++)

    Hello again,

    First, thanks Dante for making this category.

    Second point, thanks Dan, I'll think I'll just change compiler as vloktoboky suggests. Btw, if one of you could tell me which compiler is great to use for compiling on Windows, rather then Visual C++ 2005 express, I would greatly appreciate.

    Finally, thanks to you too vloktboky for your code, unfortunately I still get the same message with the copy() function, any idea please ?

     

    Thanks all

  •  12-09-2006, 10:19 AM 325 in reply to 324

    Re: Question about #define (C++)

    The copy function is the same issue as the sprintf one is.  If you wish to switch compilers, you can either try rolling back to 2003 or you can use Dev C++, a free compiler that is what it is: a free compiler.
  •  12-09-2006, 4:37 PM 326 in reply to 325

    Re: Question about #define (C++)

    vloktboky:
    The copy function is the same issue as the sprintf one is.  If you wish to switch compilers, you can either try rolling back to 2003 or you can use Dev C++, a free compiler that is what it is: a free compiler.

     2005 has a rollback feature in the options menus. Look around.

     

    Also, if you wish to disable MS warnings, you can use the following code just after your pound defines:

     #pragma once


    Freelance Writer now that I am canned...
    Renevo Software and Designs
  •  12-10-2006, 6:18 AM 329 in reply to 326

    Re: Question about #define (C++)

    I tried #pragma once, but i still get the warnings. Otherwise on Dev c++, it's weird, I just can't get my socket script working on it, when it does with Visual C++ 2005, even tough I added the libraries. I prefer Visual for the colors, but I might just go on Linux so anyway.

    I didn't find for the rollback feature :/

     

    Thanks for the help

  •  12-10-2006, 10:40 AM 333 in reply to 329

    Re: Question about #define (C++)

    If you are just getting warnings and your code is compiling without errors, ignore them.  You can turn them off through a #pragma warning call.  It's bad practice to outright disable a warning message, but these ones in particular, nobody will judge.
  •  12-10-2006, 3:54 PM 338 in reply to 333

    Re: Question about #define (C++)

    I agree with you, but one thing is annoying: When I get errors, or new warnings, it messes up with the old ones. I mean it gets harder for me to read, it would just be better if they didn't showed up lol, but i will live with it Wink

    Thank you

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems