Board index » delphi » Overwriting a few bytes in very large file

Overwriting a few bytes in very large file

Quote
On Thu, 17 Jul 1997 15:33:21 GMT, james...@usa.net wrote:
>I am writing a database programme, it is require to handle a very
>large database file (about 10 MB).
>It is quite hard for me to change only a few bytes in file. It is
>because the method I use is linear loading.

As long as you are not talking about deleting or inserting bytes into
the file, you can use the "untyped file" approach to read and write
bytes at specific locations in a file.  Look up the online help for
"BLOCKREAD" and "BLOCKWRITE".
 

Re:Overwriting a few bytes in very large file


In article <33cf3aed.7865...@news.asiaonline.net>, james...@usa.net
writes

Quote
>Hello, there!

>I am writing a database programme, it is require to handle a very
>large database file (about 10 MB).
>It is quite hard for me to change only a few bytes in file.

If you only want to *change* bytes rather than insert or delete them,
you can use Seek to get to the right part of the file, BlockRead the
data, change the relevant data then use Seek and BlockWrite to write the
amended data to the file. The file will need to be an untyped File or a
File of ....

HTH
--
Pedt Scragg <postmas...@pedt.demon.co.uk>

In principle,   is there uncertainty that
Heisenberg was working his best in chaos?

Re:Overwriting a few bytes in very large file


Hello, there!

I am writing a database programme, it is require to handle a very
large database file (about 10 MB).
It is quite hard for me to change only a few bytes in file. It is
because the method I use is linear loading.
For Example, my db file have 6 element ABCDEF,  If I want to overwrite
E by Z (ABCDZF),
I need to
1, Read ABCD
2, Write ABCD
3, Write Z
4, skip reading E
5, Read F
6, Write F
7, Close the File
It is no problem, if the Database is so small, but my is too large for
that operation, If everytime I just change 3 bytes in 10MB file at 9MB
after the head of file. It will be too time consuming.
Is it possible to skip operation [1,2,5,6]?
Thanks for reading

James.
[james...@usa.net]

Re:Overwriting a few bytes in very large file


Quote
> I am writing a database programme, it is require to handle a very
> large database file (about 10 MB).
> It is quite hard for me to change only a few bytes in file. It is
> because the method I use is linear loading.
> For Example, my db file have 6 element ABCDEF,  If I want to overwrite
> E by Z (ABCDZF),
> I need to
> 1, Read ABCD
> 2, Write ABCD
> 3, Write Z
> 4, skip reading E
> 5, Read F
> 6, Write F
> 7, Close the File
> It is no problem, if the Database is so small, but my is too large for
> that operation, If everytime I just change 3 bytes in 10MB file at 9MB
> after the head of file. It will be too time consuming.
> Is it possible to skip operation [1,2,5,6]?

   Yes: use a file of Byte (or char); Open with Reset; Seek to the byte
position you want (0 based addressing); Write the character; Close the
file.

Re:Overwriting a few bytes in very large file


In article <33cf3aed.7865...@news.asiaonline.net>, james...@usa.net
writes

Quote
>I am writing a database programme, it is require to handle a very
>large database file (about 10 MB).

Eeek... I don't envy you having to do all the testing on such large
files.

Quote
>It is quite hard for me to change only a few bytes in file. It is
>because the method I use is linear loading.
>For Example, my db file have 6 element ABCDEF,  If I want to overwrite
>E by Z (ABCDZF),

Uh huh...

Quote
>It is no problem, if the Database is so small, but my is too large for
>that operation, If everytime I just change 3 bytes in 10MB file at 9MB
>after the head of file. It will be too time consuming.
>Is it possible to skip operation [1,2,5,6]?

If your elements are a fixed size (all the same and predefined - like
bytes) then you could try the Seek procedure to move to the start of an
element you wish to overwrite. Then just use the normal write command
and it should overwrite the stored element. Then close the file.

If you use Seek you might have to use an extra parameter on Reset(file)
e.g Reset(file,1) which helps Turbo Pascal with reading/writing. See the
online help for more details.

This is sometimes called updating 'in situe'.

If your not using Turbo Pascal then I don't know how to help. TP enables
you too treat even serial/sequential files etc. with an element of
random access.

Good luck,

Karl
---
Karl Dyson [ka...@feklore.demon.co.uk]
"That spinning discus punch... usually you commit
  suicide after that - but not here!"

Re:Overwriting a few bytes in very large file


If I make use of BlockRead and BlockWrite, I need a unsigned file
handle, then a new problem is happen.

If I need to replace locate 1024 to 1025's data from the beging of
file to become 'XX', but since it is an unsigned file handle, should I
write the data as Char or Integer? (Is it something like
write(f,'XX')), what actually should I do? I have already try my best
to learn from the on-line help, but I do not give any explaination on
changing the read in to write out data. It just show me how to use
BlockRead and BlockWrite to copy a file.

Thanks for helping.

yours,
James
[james...@usa.net]

Re:Overwriting a few bytes in very large file


Quote
On Fri, 18 Jul 1997 00:06:21 GMT, james...@usa.net wrote:
>If I make use of BlockRead and BlockWrite, I need a unsigned file
>handle, then a new problem is happen.

It's an "untyped" file, not an "unsigned file handle".

Quote
>If I need to replace locate 1024 to 1025's data from the beging of
>file to become 'XX', but since it is an unsigned file handle, should I
>write the data as Char or Integer? (Is it something like
>write(f,'XX')), what actually should I do? I have already try my best
>to learn from the on-line help, but I do not give any explaination on
>changing the read in to write out data. It just show me how to use
>BlockRead and BlockWrite to copy a file.

You can use whatever you feel is the most appropriate. That's one of
the advantages of reading/writing to an untyped file.  I'd be inclined
to use an array of two characters.  The steps involved would go
something like:

1. Assign the file name to the untyped file variable.

2. Reset the untyped file variable with a record length of 1.
(Important!)

3. Seek to position 1023 in the file.

4. Set my two character array to 'XX'.

5. BlockWrite 2 bytes from the array to the file.

6. Close the file.

Re:Overwriting a few bytes in very large file


Quote
james...@usa.net wrote:
> If I make use of BlockRead and BlockWrite, I need a unsigned file
> handle, then a new problem is happen.

> If I need to replace locate 1024 to 1025's data from the beging of
> file to become 'XX', but since it is an unsigned file handle, should I
> write the data as Char or Integer? (Is it something like
> write(f,'XX')), what actually should I do? I have already try my best
> to learn from the on-line help, but I do not give any explaination on
> changing the read in to write out data. It just show me how to use
> BlockRead and BlockWrite to copy a file.

Using blockread() and blockwrite() *aren't* necessary, if it helps to
avoid confusion.  The rules that apply to untyped files for read/write
access also applies to typed files.  For example, examine, compile, and
run this program:

program randomtest;

var
  f : file of integer;
  i, n : integer;

begin
  {create a data file}
  assign (f,'integer.dat');
  rewrite (f);
  for i := 1 to 10 do
    begin
      n := i*10;
      write (f,n);
    end;
  close (f);
  {file closed, now open it for read/write}
  filemode := 2; {ensures read/write (compatibility) mode}
  reset (f); {NOTE:  do NOT use rewrite on the existing file!}
  seek (f,7); {jump to the 8th record}
  n := 12345;
  write (f,n);
  close (f);
  {now just read from the file only.}
  reset (f);
  while not eof(f) do
    begin
      read (f,n);
      write (n:8);
    end;
  writeln;
  close (f);
end.

Hopefully this will help with your problem?

Quote
> Thanks for helping.

> yours,
> James
> [james...@usa.net]

--
Scott Earnest        | We now return you to our regularly |
set...@ix.netcom.com | scheduled chaos and mayhem. . . .  |

Re:Overwriting a few bytes in very large file


Quote
james...@usa.net wrote:

>If I make use of BlockRead and BlockWrite, I need a unsigned file
>handle, then a new problem is happen.

>If I need to replace locate 1024 to 1025's data from the beging of
>file to become 'XX', but since it is an unsigned file handle, should I
>write the data as Char or Integer? (Is it something like
>write(f,'XX')), what actually should I do? I have already try my best
>to learn from the on-line help, but I do not give any explaination on
>changing the read in to write out data. It just show me how to use
>BlockRead and BlockWrite to copy a file.

Hello James,

The BlockWrote/Read procedures are used to copy memory images
to and from disk.  In other words, if you BlockWrite from an
array of char then the data appears in the file in the same
order that it appeared in memory.  The same holds if you from
an integer.  In this case you must realize that an integer is
stored in memory as a binary value with the least significant
byte first and the most significant byte last.  However, as
long as you use the same type of variable the data will be
read from the file in the same order that it was written.
Since it makes no difference to the program, you only need to
be concerned with any requirements that dictate how the data
must appear in the file.  If you aren't sure, a good rule
would be to use the same type of variable to write to the file
that you use when reading from the file.

In your original post you ask how to manage a large database,
but other than saying that you are currently processing the
file in a sequential manner, you say nothing about the
structure of the database itself.  Since you state nothing
about the file or its data, the single best guess is to use an
untyped file.  The answers you've received are based on your
example of needing to change two bytes within a file of
unknown structure.  

OTOH, if the file is composed of fixed length records, then
the best solution might be to use a file of record and to seek
to the record to be changed.  

The suggestions you receive can only be as good as the
information you provide us.

    ...red

--
Support the anti-Spam amendment
  Join at http://www.cauce.org/

Other Threads