Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
auth_update crash
Dec 23, 2017, 10:15 pm
By Remcon
check_tumble
Dec 18, 2017, 7:21 pm
By Remcon
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Users Online
CommonCrawl, Bing, Yahoo!, Yandex

Members: 0
Guests: 7
Stats
Files
Topics
Posts
Members
Newest Member
478
3,708
19,242
612
Jacki72H
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » General » Coding » C++ File Streams
Forum Rules | Mark all | Recent Posts

C++ File Streams
< Newer Topic :: Older Topic > Is there a better way?

Pages:<< prev 1 next >>
Post is unread #1 Nov 27, 2007, 5:50 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

As part of a C++ conversion I've been writing routines like this to replace the simpler file loaders for AFKMud. Since doing this kind of conversion is likely to be of interest to many others, I wondered if perhaps the methods I've chosen are the best. This works, but I'm left with the nagging feeling like there's a better way to make use of C++ file streams.

void load_landmarks( void )
{
   char filename[256];
   landmark_data *landmark;
   ifstream stream;

   landmarklist.clear(  );

   snprintf( filename, 256, "%s%s", MAP_DIR, LANDMARK_FILE );
   stream.open( filename );
   if( !stream.is_open() )
      return;

   do
   {
      string line, key, value;
      char buf[MIL];

      stream >> key;
      stream.getline( buf, MIL );
      value = buf;

      strip_lspace( key );
      strip_tilde( value );
      strip_lspace( value );

      if( key == "#LANDMARK" )
         landmark = new landmark_data;
      else if( key == "Coordinates" )
      {
         string arg;

         value = one_argument( value, arg );
         landmark->map = atoi( arg.c_str() );

         value = one_argument( value, arg );
         landmark->mx = atoi( arg.c_str() );

         value = one_argument( value, arg );
         landmark->my = atoi( arg.c_str() );

         landmark->distance = atoi( value.c_str() );
      }
      else if( key == "Description" )
         landmark->description = value;
      else if( key == "Isdesc" )
         landmark->Isdesc = atoi( value.c_str() );
      else if( key == "End" )
         landmarklist.push_back( landmark );
      else
         log_printf( "Bad line in landmark data file: %s %s", key.c_str(), value.c_str() );
   }
   while( !stream.eof() );
   stream.close();
}
       
Post is unread #2 Nov 27, 2007, 6:47 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

Seems like an ok use of streams, although you might want some form of error checking. I.e.,

if (stream.fail()) { /* do something */ }

This call probably isn't as efficient as it could be:

value = one_argument( value, arg );

because you have to copy the result of value back into itself. It would be more efficient to break the string into a vector once, probably, than to keep copying the string. But the difference is basically irrelevant during file-load. It's not worth worrying about, I think.
       
Post is unread #3 Nov 27, 2007, 6:53 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

Is this not going to work for failure to open?

if( !stream.is_open() )
      return;
       
Post is unread #4 Nov 27, 2007, 7:15 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

That will catch failure to open but not various failures that could occur during reading. For instance, you might want to know if an error occurred while reading the 'value' line so that you don't try one_argument'ing an invalid line into pieces.
       
Pages:<< prev 1 next >>