From
Amcleod@VERT to
Jim King on Mon Apr 2 12:34:02 2001
RE: File access - BAJAãBY: Jim King to ALL on Mon Apr 02 2001 06:12 pmãã > Would someone be willing to write and explain a simple (short) programã > that reads from and writes to a file in Baja?ã > ã > Use as many of the file accessing routines found in Baja.ã > ã > From Baja Docs:ã > ã > FOPEN <int_var> <#> <"str" or str_var> FILE_IO.INCã > args: handle access path_and_filenameã > ã > FREAD <int_var> <any_var> [int_var or #]ã > args: handle dest lengthã > ã > FCLOSE <int_var>ã > args: handleããEssentially you have it covered, except: I'd recommend first a program thatãopened the file, WROTE some data and then closed it. You decide what goesãinto the file, what the format is, etc, etc, and you should have a pretty goodãidea what to expect in that file when the program has executed. ããTHEN your SECOND program essentially does the opposite. Open the same file,ãread back the data and display it to confirm that you i) wrote it correctly;ãii) read it back as expected.ããDo you understand the arguments to the commands listed above? ("handle",ã"access", etc) Or do you need someone to run through them with you first?ã---ã þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.netã
From
Amcleod@VERT to
Jim King on Tue Apr 3 02:21:34 2001
RE: File access - BAJAãBY: Jim King to Amcleod on Tue Apr 03 2001 06:17 amãã > Knowing all this... how would I translate ... say "xtrn->code 9" into aã > fread statement? Using "%!\..\crtl\xtrn.cnf" as the source.ããOK Jim, here's an example of accessing XTRN.CNF via BAJA. I don't know if itãwill lead you where you want to go or not. But either way, here it is, RAABAãand not particularly well testedãã# We need the standard definitions in "FILE_IO.INC" so...ã!include FILE_IO.INCãã# define some variables we will need laterãint FHnd Offs XProgCt XP_Secãstr FName XP_COde XP_Name XP_Cmdãã# define name of config file (use a copy while debugging!)ãset FName "/sbbs/ctrl/x_xtrn.cnf"ãã# open the file for reading, and get back a unique integer "handle" thatã# allows us to refer to this open file in all subsequent operations. Bombã# if file fails to openãfopen FHnd O_RDONLY FNameãif_falseã print "fopen() failed!\r\n";ã pauseã exitãend_ifãã# Refer to XTRN.CNF section of CNFS.FMT for details on file layout!ã# First two bytes in file is "total_swaps", read this (no error checkingã# from now on; duplicate if_false/end_if from above and change as needed).ãfread FHnd Offs 2ãã# "total_swaps" is followed by that many "swap_cmd" at 64 bytes each,ã# so compute how many bytes must be skipped to come to "External Editors"ã# and reposition file pointer to that offset.ãmul Offs 64ãfset_pos FHnd Offs SEEK_CURãã# Now read "total_xedits". Skip past 41+9+64+64+4+41+16 = 239 timesã# "total_xedits" bytes to get to "External Program Section"ãfread FHnd Offs 2ãmul Offs 239ãfset_pos FHnd Offs SEEK_CURãã# Find out how many "total_xtrnsecs" and compute offset to "Externalã# Program" (41+9+41+16 = 107)ãfread FHnd Offs 2ãmul Offs 107ãfset_pos FHnd Offs SEEK_CURãã# Phew! Finally pointing at "External Programs". How many in total?ãfread FHnd XProgCt 2ãã# This is the top of a loop that will iterate over the list of "Externalã# Programs", processing each one as we go.ã:NextXPãã # If there are ZERO (or less ?!?) "External Programs" left, weã # are finished processing the list and wanna call it quits.ã compare XProgCt 0ã if_less_or_equalã goto WereDoneã end_ifãã # Since we aren't done, print some basic info on the nextã # "External Program".....ãã # Read some data...ã fread FHnd XP_Sec 2ã fread FHnd XP_Name 41ã fread FHnd XP_Code 9ãã # Skip 41+41+1+4+1+4 = 92 bytes we're not interested inã fset_pos FHnd 92 SEEK_CURãã # Read some more dataã fread FHnd XP_Cmd 64ãã # Skip 64+64+16 = 144 remaining (unwanted) bytes for thisã # "External Program"ã fset_pos FHnd 144 SEEK_CURãã # Print out the info we picked up:ã printf "%5ld -- %-8s -- %-40s\r\n" XP_Sec XP_Code XP_Nameã printf " %-64s\r\n\r\n" XP_Cmdãã # Reduce count by one and loooooop!ã sub XProgCt 1ã goto NextXPãã# OK, this is where we come to when there are no more "External Programs"ã# for us to process...ã:WereDoneãprint "We're done!\r\n"ãã# Ok, we're done, so courteously close file before exitingãfclose FHndãã# Sayonara!ãexitããThere you have it; RAABA as promised! Read it through step by step referringãto the CNFS.FMT file (XTRN.CNF section) and you should be able to easily seeãwhat is happening. As usual, programming turns out to be a boring, ste-by-stepãexercise.ããTo make _changes_ to the file, you essentially follow a similar process toãthis: Carefully compute the location of the byte(s) you want to change,ãpossibly reading data and computing offsets to get there. When your fileãpointer is aimed exactly at where you want the data put, then you fwrite() theãexact number of bytes into the file, then reposition the file pointer for theãnext write. You have to open the file Read/Write instead of Read-Only, ofãcourse. Make _sure_ to use a copy of the file when debugging to avoid hosingãyour valuable config data!ã---ã þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.netã