scanf_s into array help

closed account (3vCpfSEw)
Hello all,
I am having a bit of trouble.

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?"

I am using using scanf_s
1
2
3
4
5
6
7
    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 put arrays trigg and alert so I can use them for this function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 on
What's the question?
closed account (3vCpfSEw)
How come it crashes? when i add the & in for the arrays

closed account (3vCpfSEw)
Also, couldn't I just do:
1
2
3
4
i=0;
for(i=0; i <500; i++)
fscanf_s(f,"%s %s",&trigg[i],&alert[i]);
i++;
¿why shouldn't crash? the array is 255 character long, but you are trying to access elements around 500.
And you are trying to put strings there, with no length limit.

1
2
3
    for(i=0; i <500; i++)
    scanf_s("%s",&trigg[i]); //this is inside the loop
    scanf_s("%s",&alert[i]); //this is outside the loop 
closed account (3vCpfSEw)
ok ne555 thanks,

and yes I am should I be using const char?


1
2
3
4
5
6
7
8
                char trigg[512];
		char alert[512];
		i=0;
		fscanf_s(f,"%s %s",trigg,alert);
		for(i=0; i <512; i++)
		scanf_s("%s",&trigg[i]);
		scanf_s("%s",&alert[i]);
		++i;



can't I just do

1
2
3
4
5
6
                char trigg[512];
		char alert[512];
                i=0;
                for(i=0; i<512; i++)
                fscanf_s(f,"\"%s\" \"%s\"",&trigg[i],&alert[i]);
                i++;
Last edited on
¿eh? ¿why const char?
"%s" is for reading strings. If you do scanf("%511s", trigg); you will read an string that is at most 511 wide (remember the '\0')*

However you are doing scanf_s("%s",&trigg[i]); so you will start reading at position 0, fill the string, and then discard everything except the first character and start reading again.

man scanf wrote:
s Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null character ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.
So it is no good for what you want. Either read a line and parse it later or use %[ format especifier.


* I don't remember if the number includes the null terminator.
closed account (3vCpfSEw)
ok so how exactly must i do this.

I want to read the text from the file

"text here" "alert here"

and put them into arrays

text[0] alert[0]
text[1] alert[1]
text[2] alert[2]
text[3] alert[3]
text[4] alert[4]
text[5] alert[5]
text[6] alert[6]

and so on. so i can add more later.
text[i] alert[i]
A char stores 1 character, not words.

If you don't know how many lines could be in the file, you may need an stl container (std::vector, by instance)
About reading the lines, maybe something like
1
2
3
std::string line;
cin.ignore( max_size, '\"'); //jump till the starting quote
getline( cin, line, '\"' ); //copy to the ending quote 
Topic archived. No new replies allowed.