1. *Do Not Read* (unless you are stumped)

below, you will find the answer (one of the potential
answers i suppose) to the "assignment" I gave about
the logfile() routine.




If you haven't solved it yet, and you are still working
on it, do not read any farther.

If you have solved it, then you may wish to look at the code
below as there is extra credit work for you :)

lastly, some of the functions/procedures are general purpose,
and if you wanna put them in your toolbox, i don't mind :)

take care--Hawke'










don't go any further now, if you haven't solved it...

















really....















are you sure you're stuck???














really sure???















last chance...















ok, here it is:




















----------code (tested) for logfile.ex (logfile.e??)
include graphics.e  --to use graphics_mode()

--******Extra Credit:
--get the value of this constant from an .ini file,
--and make sure to place the value into a *constant*...
constant LOGFILENAME="badcheck.log"

procedure CriticalError(sequence message)
--halts execution upon a nonrecoverable error, restores
--the video mode (in case it was changed), and prints
--the error message "message".
   if graphics_mode(-1) then end if
   clear_screen()
   puts(1, "\n" & message &"\n")
   abort(1)
end procedure

function FixYear(integer year)
--takes the numeric representation of a year, as returned
--by date(), which is from 1900, and returns 4 digit year

  return year + 1900
end function

function GetDateandTime()
--returns the date and time as a string as such:
--   January 12, 1998 @ 19:43 (military time)

sequence temp,MONTHNAME
integer YEAR,MONTH,DAY,HOUR,MINUTE --locations! within date()

   --we create MONTHNAME, YEAR,... here, within a function,
   --so that we use less memory by not having them as global...
   MONTHNAME  ={"January",  "February","March",   "April",
                "May",      "June",    "July",    "August",
                "September","October", "November","December"}
   YEAR =1  MONTH=2  DAY=3  HOUR=4  MINUTE=5

   temp         = date()
   temp[YEAR]   = FixYear(temp[YEAR])
   temp         = MONTHNAME[temp[MONTH]]    & " "   &
                sprintf( "%d",temp[DAY]   ) & ", "  &
                sprintf( "%d",temp[YEAR]  ) & " @ " &
                sprintf( "%d",temp[HOUR]  ) & ":"   &
                sprintf( "%d",temp[MINUTE]) & " (military time)"

   return temp
end function

procedure logfile(sequence logline)
--writes the 'string' "logline" to the bottom of the
--file by the name of LOGFILENAME

integer handle --(ie:file number)

   --"a" is append, autocreates the file if it doesn't exist
   handle=open(LOGFILENAME,"a")
   if handle = -1 then
        CriticalError("Cannot open " & LOGFILENAME & "!" )
   else
        puts (handle, GetDateandTime() & "\n"  )
        puts (handle, logline          & "\n\n")
        close(handle)
   end if

end procedure

-----------------MAIN
logfile("this is a test")
logfile("this is test two")

--------------------end code

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu