Server Help

Bot Questions - file reading

Cheese - Sun Nov 25, 2007 3:54 am
Post subject: file reading
i was looking to write a dll for mervbot.
just a simple staff list from staff.txt...

Code: Show/Hide

void botInfo::gotCommand(Player *p, Command *c)
...
case OP_Player:
      {
         if (c->check("staff"))
         {
            sendPrivate(p, "start");
            char line [256];
            ifstream staff;
            staff.open("staff.txt");
            if (!staff)
            {
               sendPrivate(p, "Error loading staff.txt");
            }
            staff>>line;
            while (!EOF)
            {
               sendPrivate(p, line);
               staff>>line;
               sendPrivate(p, "loop");
            }
            staff.close();
            sendPrivate(p, "end");
         }


i added start, loop, and end for debugging.
so it compiles and loads, but only pms you start and end.
which means its completely ignoring my loop.

also, when i remove staff.txt, theres no error.
which means its ignoring my error too.

what am i missing?
k0zy - Sun Nov 25, 2007 6:46 pm
Post subject:
try
Code: Show/Hide
while (!staff.eof())

Bak - Sun Nov 25, 2007 7:00 pm
Post subject:
if you remove staff.txt it doesn't crash? try making the condition if (!staff.good()) and put the rest of the crap in an else block
Cheese - Sun Nov 25, 2007 8:17 pm
Post subject:
for some reason, those dont work =S
mine dont either...

also,
why can i use sendPrivate(p, "loop");
with "loop" and not a string?
using char ignores whitespace in my file...

----

so all that was too complicated and didnt work.
i tried this, and it worked.
Code: Show/Hide
case OP_Player:
      {
         if (c->check("about"))
         {
            sendPrivate(p, "I display the staff list.");
         }
         if (c->check("version"))
         {
            sendPrivate(p, "staff.dll by Cheese");
         }
         if (c->check("staff"))
         {
            ifstream staff;
            staff.open("staff.txt");
            char line [256];
            while (staff.getline(line, 256))
            {
               sendPrivate(p, line);
            }
            staff.close();
         }
      }
however,
Code: Show/Hide
            if (staff.bad())
            {
               sendPrivate(p, "Staff.txt is missing");
            }
didnt.
renaming or deleting the file didnt make anything happen.
i tried staff.bad(), !staff.good(), !staff.exist(), and !staff.
nothing at all
k0zy - Mon Nov 26, 2007 5:02 am
Post subject:
What about staff.is_open() ?
Bak - Mon Nov 26, 2007 8:06 am
Post subject:
sendPrivate needs a String, which is one of catid's classes and not built-in. If you look in mervbot source you'll see theres a constructor that takes in a const char* so it can cast it implicitly.

If you want to use a string, try

sendPrivate(p,(String)stringVar.c_str());
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group