Thread: scanf_s into array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    scanf_s into array

    Hello all,
    James here, I had a question.

    I am using using scanf_s

    Code:
        char trigg[255];
        char alert[255];
        i=0;
        for(i=0; i <500; i++)
        scanf_s("%s",&trigg[i]);
        scanf_s("%s",&alert[i]);
        i++;
    Trying to read a text file with contents:

    "trigger for the bot to see" "alert for the bot to make for trigger"
    "LOL" "Laughing Out Loud"
    "Hello" "Hello world!"
    "Hi there" "Hello, how are you?"


    Into arrays trigg and alert so I can use them for this function

    Code:
    void bot_manager_item::on_push_event(bot_exchange_format f)
    {
        switch (f.pid)
        {
        case BOT_EVENT_IM:
            {
                std::string nickname = std::string(f[0x01]);
                int feedback = (int)f[0x02];
                u_char age = (u_char)f[0x03];
                std::string text = std::string(f[0x04]);
                u_char attributes = (u_char)f[0x05];
                u_char size = (u_char)f[0x06];
                u_long color = (u_long)f[0x07];
                u_long effects = (u_long)f[0x08];
                u_char charset = (u_char)f[0x09];
                u_char pitch = (u_char)f[0x0A];
                std::string font = (std::string)f[0x0B];
    
    
                if (!text.empty())
                {
                    transform(text.begin(), text.end(), text.begin(), toupper);
    
    
                    if (!strcmp(text.c_str(), "/VER"))
                    {
                        bot_exchange_format p(PLUGIN_EVENT_IM);
                        p << bot_value(0x01, nickname);
                        std::string text = ("James' Camfrog Bot 5.1 Sample Plugin");
                            
                        p << bot_value(0x02, text.c_str() );
                        p << bot_value(0x03, attributes);
                        p << bot_value(0x04, size);
                        p << bot_value(0x05, color);
                        p << bot_value(0x06, effects);
                        p << bot_value(0x07, charset);
                        p << bot_value(0x08, pitch);
                        p << bot_value(0x09, font);
    
    
                        std::string d = p.data();
                                        
                        _mngr->deliver_event(_name.c_str(), d.c_str(), (int)d.size());
                    }
                }
            } break;
    
    
        case BOT_EVENT_ROOM_TEXT:
            {
                std::string nickname = std::string(f[0x01]);
                std::string text = std::string(f[0x02]);
                u_char attributes = (u_char)f[0x03];
                u_char size = (u_char)f[0x04];
                u_long color = (u_long)f[0x05];
                u_long effects = (u_long)f[0x06];
                u_char charset = (u_char)f[0x07];
                u_char pitch = (u_char)f[0x08];
                std::string font = (std::string)f[0x09];
    
    
    
    
                if (!text.empty())
                {
                    transform(text.begin(), text.end(), text.begin(), toupper);
                    
                    if (!strcmp(text.c_str(), ".&trigg[0]."))
                    {
                        bot_exchange_format p(PLUGIN_EVENT_ROOM_TEXT);
                        p << bot_value(0x01, ".&alert[0].");
                        p << bot_value(0x02, attributes);
                        p << bot_value(0x03, size);
                        p << bot_value(0x04, color);
                        p << bot_value(0x05, effects);
                        p << bot_value(0x06, charset);
                        p << bot_value(0x07, pitch);
                        p << bot_value(0x08, font);
    
    
                    std::string d = p.data();
                                        
                    _mngr->deliver_event(_name.c_str(), d.c_str(), (int)d.size());
                    }
                }
            } break;
    I thought I had the right idea. Any pointers would be great.
    Last edited by JamesY; 11-02-2011 at 05:21 AM.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    I think this belongs in the C++ forum.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Moved
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you're using std::string everywhere else, why not use it while reading in the file? use a std::vector of std::string objects, and try to stay away from C-style programming techniques when writing C++ code.

    use std::getline to read from a file directly into a std::string. scanf, and even scanf_s (which you have actually used incorrectly here) are unsafe, and reading directly into character arrays without specifying the size of those arrays in the scanf call is asking for trouble.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. fscan skipping a character
    By Axel in forum C Programming
    Replies: 7
    Last Post: 10-17-2005, 10:47 AM
  5. Problem with fseek(), fprintf(), fscan()
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 10:31 PM