• Welcome to Valhalla Legends Archive.
 

File manipulation

Started by Eli_1, August 08, 2004, 02:44 PM

Previous topic - Next topic

Eli_1

It seems like I'm going through way too much work to simply "remove" a line of text form a file. I was originally planning on using that "redim" deal I posted about, but my compiler gives me **** when I try to create/alloc memory without using a constant.


Here's how I'm doing it now.


bool RemoveUser(char *Data)
{
   FILE       *pfSrc    = fopen("Database.txt", "r");
   FILE       *pfDest   = fopen("SWAP0001", "w");

   char       Buffer[128];
   char      tmpUser[128];
   char       *p;

   signed int   tmpAccess = 0;


   if (!pfSrc || !pfDest)
   {
      fclose(pfSrc);
      fclose(pfDest);
      return false;
   }
      
      
   while (fgets(Buffer, 128, pfSrc))
   {
      p = strchr(Buffer, '=');
      if (p)
      {
         *p = 0;
         strcpy(tmpUser, Buffer);
         *p++;
         tmpAccess = atoi(p);

         if (stricmp(tmpUser, Data) != 0)
         {
            sprintf(Buffer, "%s=%d", tmpUser, tmpAccess);
            fprintf(pfDest, "%s\n", Buffer);
         }
      }
   }

   fclose(pfDest);
   fclose(pfSrc);
   
   DeleteFile("Database.txt");
   MoveFile("SWAP0001", "Database.txt");

   return true;
}


I'm worried this method is prone to failure. Any thoughts on a better way, or do you think this is safe enough?

Mephisto

I don't really have time to look at yours atm (gotta get going in a minute), but here's what I had to do to remove a user from my bot database and is an example of "how much work" you have to do to remove some text from a specified random part of a file...

               ofstream fout("database.txt");
               std::map < string, user_data >::const_iterator pos;
               for (pos = UserMap.begin(); pos != UserMap.end(); pos++) {
                  fout << UserMap[pos->first].username << endl;
                  fout << UserMap[pos->first].flagstring << endl;
                  fout << UserMap[pos->first].flags << endl;
                  fout << UserMap[pos->first].lastaction << endl;
                  fout << UserMap[pos->first].lastseen << endl;
                  fout << endl;
               }


Just rewrite the file and explicitly take out the line of text (multiple lines) from what you're writing.  For instance, in this map the map is and it's user_data struct contents are written for to the file for each user entry in the map.  Just remove a user from the map to remove those line(s) of text.  :)

I'll look at yours later, which I'm sure is probably quite different than what any example that provided as.  Cya :P

K

Why not simply make the changes in memory and then write the changes when your bot shuts down, or when it is idling?  Then you can simply truncate the file and write what you have in memory.

Mephisto

You're not really going through much more than you have to do to remove a line of text from the file (or edit, add to a specific part, etc.).  You just need to think about how you want to do it, and do it.  I would've done it differently, but that's your way.  There's no special functions or anything that can do that kind of thing for you, since most of those situations rely on how you have it set up.  :)