1. Writing records into fixed length record file....

Will it probably be my misfortune to discover that I cannot use puts()
to write a string into a fixed length file (as in update or replace data)
because puts() will append a trailing character(s) and mess up the file.
Looping thru putc() is kind of undesireable for many records. 
There seems to be no counter-part to 'get_bytes' to do writes to a 
fixed length file with fixed length records .... i.e. 'put_bytes' ??

new topic     » topic index » view message » categorize

2. Re: Writing records into fixed length record file....

John F Dutcher wrote:
> 
> 
> Will it probably be my misfortune to discover that I cannot use puts()
> to write a string into a fixed length file (as in update or replace data)
> because puts() will append a trailing character(s) and mess up the file.

And what trailing character(s) might that be?

new topic     » goto parent     » topic index » view message » categorize

3. Re: Writing records into fixed length record file....

John F Dutcher wrote:
> 
> 
> Will it probably be my misfortune to discover that I cannot use puts()
> to write a string into a fixed length file (as in update or replace data)
> because puts() will append a trailing character(s) and mess up the file.
> Looping thru putc() is kind of undesireable for many records. 
> There seems to be no counter-part to 'get_bytes' to do writes to a 
> fixed length file with fixed length records .... i.e. 'put_bytes' ??

Actually you *can* use puts() to do this. 

  fh = open("thefile.dat", "ub")
  . . .
  pos_to_update = (recnum - 1) * recsize
  VOID = seek(fh, pos_to_update)
  newrec = get_bytes(fh, recsize)  -- Read in existing record.
  newrec[x..y] = whatever --- Fill in the 'fields' in the record.
  . . .
  VOID = seek(fh, pos_to_update)
  puts(fh, newrec)

  
-- 
Derek Parnell
Melbourne, Australia

new topic     » goto parent     » topic index » view message » categorize

4. Re: Writing records into fixed length record file....

I am harboring what I hope is the misguided illusion...that in 'C' fashion,
puts() will append some sort of string terminating character when
issued...disturbing the 1st character of the next sequential record.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Writing records into fixed length record file....

This seems like 'the way' to provide record number access to fixed length
records in a file that is not a series of strings with a terminating end-of
-string  character (line feed..whatever).

But isn't the final puts() going to overwrite the 1st character of the
following record (they are expected to be back to back, adjacent) when
issued ?

new topic     » goto parent     » topic index » view message » categorize

6. Re: Writing records into fixed length record file....

You should be fine if the file is written to in binary mode.
The library.doc file's entry "<open>" says that I/O to binary files are
not modified in any way; wheras carrige returns are added on write, and 
stripped on read for text (non-binary) files.
Has worked as documented for me.

Signature: 
>From BC's "The curse exchange", by Johnny Hart:
"May your only son, the political candidate, be caught in the company of a
nefarious mud wrestler 3 days before the election"
"May the fleas of a thousand camels infest your armpits"

new topic     » goto parent     » topic index » view message » categorize

7. Re: Writing records into fixed length record file....

That's great....just what I needed to hear.

new topic     » goto parent     » topic index » view message » categorize

8. Re: Writing records into fixed length record file....

John F Dutcher wrote:
> 
> I am harboring what I hope is the misguided illusion...that in 'C' fashion,
> puts() will append some sort of string terminating character when
> issued...disturbing the 1st character of the next sequential record.

Yes, this is misguided. :)  Not sure what 'C' fashion is, unless you're 
talking about text mode output under Windows/DOS, where /n -> /n/r.  Also,
in text mode, you can't output EOF (26).  Just make sure you open in 
binary mode, and you shouldn't have any problems.

Matt Lewis

new topic     » goto parent     » topic index » view message » categorize

9. Re: Writing records into fixed length record file....

Matt Lewis wrote:

<snip>

>talking about text mode output under Windows/DOS, where /n -> /n/r. 
>
</snip>

Actually, text mode output under Windows/DOS where \n -> \r\n

Mac is \n\r
Linux is \n

Just so ya know. blink

Mario

new topic     » goto parent     » topic index » view message » categorize

10. Re: Writing records into fixed length record file....

John F Dutcher wrote:
> 
> I am harboring what I hope is the misguided illusion...that in 'C' fashion,
> puts() will append some sort of string terminating character when
> issued...disturbing the 1st character of the next sequential record.

Don't know why. Have you actually tried to do it yet? Here is some
example code you can try...

include file.e
include get.e
integer fh
integer recsize
sequence recdata
recsize = 57


function ReadRecord(integer pFH, integer pRecNo)
    if seek(pFH,  (pRecNo-1) * recsize) = 0 then
        return get_bytes(pFH, recsize)
    else
        return {}
    end if
end function

procedure UpdateRecord(integer pFH, integer pRecNo, sequence pData)
    if seek(pFH,  (pRecNo-1) * recsize) = 0 then
        puts(pFH, pData)
    end if
end procedure

-- Open the file in *binary* mode
fh = open("the.dat", "ub")

-- Build a test file with twenty records, each 57 bytes long.
for i = 1 to 20 do
    puts(fh, repeat('a' + i - 1, recsize))
end for

-- Fetch the fifth record.
recdata = ReadRecord(fh, 5)
-- display it
puts(1, recdata & '\n')

-- update some bytes in the record.
recdata[5 .. 9] = "xyzzy"

-- write it back to disk
UpdateRecord(fh, 5, recdata)

-- re-read it and display it again.
recdata = ReadRecord(fh, 5)
puts(1, recdata & '\n')

-- that's it.
close(fh)


-- 
Derek Parnell
Melbourne, Australia

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu