openfile: fixed record length file

Previous topic - Next topic

srj

Does GLBASIC support fixed length file creation for random access rather than sequential access? If yes, please give an example of opening a file (and reading a record) where all the records are of the same length (fixed length records). Here is an example of opening a fixed record length of 1024 file for random access (in CBasic language): OPEN "datafile.dat" RECL 1024 AS fileId%

MrTAToad

This is an example of random access (requires my memory routine) :

Code (glbasic) Select
LOCAL ptr%,handle%,loop%,loop2%

ptr%=AllocMem(1024)

handle%=OPENFILE(1,"test.dat",0)
FOR loop%=0 TO 3
FillMemory(ptr%,1024,loop%)
FOR loop2%=0 TO 1024-1
WRITEBYTE handle%,peekB(ptr%+loop2%)
NEXT
NEXT
CLOSEFILE handle%

FillMemory(ptr%,1024,255)
handle%=OPENFILE(1,"test.dat",1)
FILESEEK handle%,2*1024,0
FOR loop2%=0 TO 1024-1
READBYTE handle%,loop%
pokeB(ptr%+loop2%,loop%)
NEXT

DEBUG "First 16 bytes : \n"
FOR loop2%=0 TO 15
DEBUG peekB(ptr%+loop2%)+" "
NEXT


I dont have an efficient way of reading or writing blocks of memory to file yet as it's never been needed before.

MrTAToad

Malloc routine :

Code (glbasic) Select
// --------------------------------- //
// Project: TestMalloc
// Start: Monday, March 02, 2009
// IDE Version: 6.174

FUNCTION dummy:
ENDFUNCTION

INLINE
}

typedef unsigned int size_t;

extern "C" void *memcpy(void *destination, const void *source, size_t num );
extern "C" void *memset(void *ptr, int value, size_t num);

template <typename type, typename val> void __poke(DWORD Addr, val Value)
{
*((type*)(Addr))=(type)(Value);
}

template <typename type> type __peek(DWORD Addr)
{
return *((type *)(Addr));
}

namespace __GLBASIC__ {
ENDINLINE

FUNCTION AllocMem%:size%
INLINE
return (DGNat) new char[size];
ENDINLINE
ENDFUNCTION

FUNCTION FreeMem%:mem%
INLINE
char *temp;

temp=(char *) mem;
delete[] temp;
ENDINLINE
ENDFUNCTION

FUNCTION ZeroMemory:mem%,size%
INLINE
memset((char *) mem,(char) 0,size);
ENDINLINE
ENDFUNCTION

FUNCTION FillMemory:mem%,size%,val%
INLINE
memset((char *) mem,(char) val,size);
ENDINLINE
ENDFUNCTION

FUNCTION CopyMemory%:store%,readFrom%,size%=-1
INLINE
char value;

if (size<0)
{
value=__peek<char>((DWORD) readFrom);
while (value!=0)
{
__poke<char>((DWORD) store,value);
readFrom++;
store++;
}

return true;
}
else
if (size>0)
{
memcpy((char *) store,(char *) readFrom,size);
return true;
}
else
{
return false;
}
ENDINLINE
ENDFUNCTION

FUNCTION pokeB:mem%,val%
INLINE
__poke<char>((DWORD) mem,char(val));
ENDINLINE
ENDFUNCTION

FUNCTION pokeS:mem%,val%
INLINE
__poke<short>((DWORD) mem,short(val));
ENDINLINE
ENDFUNCTION

FUNCTION pokeL:mem%,val%
INLINE
__poke<int>((DWORD) mem,val);
ENDINLINE
ENDFUNCTION

FUNCTION pokeF:mem%,val
INLINE
__poke<float>((DWORD) mem,(float) val);
ENDINLINE
ENDFUNCTION

FUNCTION pokeDF:mem%,val
INLINE
__poke<double>((DWORD) mem,val);
ENDINLINE
ENDFUNCTION

FUNCTION pokeString:mem%,str$
INLINE
memcpy((char *) mem,str_Str.c_str(),str_Str.len());
ENDINLINE
ENDFUNCTION

// ------------------------------------------------------------------------------------------------------------------------------------
FUNCTION peekB%:mem%
INLINE
return ((DGNat) (__peek<char>(mem)));
ENDINLINE
ENDFUNCTION

FUNCTION peekS%:mem%
INLINE
return ((DGNat) (__peek<short>(mem)));
ENDINLINE
ENDFUNCTION

FUNCTION peekL%:mem%
INLINE
return ((DGNat) (__peek<int>(mem)));
ENDINLINE
ENDFUNCTION

FUNCTION peekF:mem%
INLINE
return ((DGInt) (__peek<float>(mem)));
ENDINLINE
ENDFUNCTION

FUNCTION peekDF:mem%
INLINE
return ((DGInt) (__peek<double>(mem)));
ENDINLINE
ENDFUNCTION

FUNCTION peekString$:mem%,length%=0
LOCAL temp$
LOCAL l%
LOCAL p%

temp$=""

IF length%>0
FOR l%=0 TO length%-1
temp$=temp$+CHR$(peekB(mem%+l%))
NEXT
ELSE
p%=peekB(mem%)
WHILE p%<>0
temp$=temp$+CHR$(p%)
INC mem%,1
p%=peekB(mem%)
WEND
ENDIF

RETURN temp$
ENDFUNCTION