Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How do I 'write' (transpose) within a file?

mydelphimydelphi Member Posts: 62
..but without reading and writing the whole file in question?

Ok. Let me explain, if I can.

Hello everyone. Its been a long time since I last singed on. Good to be
back. Well, I have a question and I need your help.

I'm trying to write to a file. Any file, since it doesn't matter at this
point in time. But, because some of the times will be very large, I do
not want to read/write in the triditional way, example:

[code]
repeat
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
BlockWrite(ToF, Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
[/code]

What I want to do is just 'write' directly to the file itself. You see,
the size is 1 byte (a number value or something else) and it only needs
to be written in one place, but the place (position) will ber user
configurable, because the position will vary from file to file. But they
only need to write one piece of data to the file. I'm using a TrackBar
control and an EditBox for the file position edits.

Sort of like this. Say you have a file who is only 8 bytes in size, and
the data in it is obviously 8 pieces of data. Can be anything, but lets
just simplify the example with a string of values who are charactors, like
this:

(A)(B)(C)(D)(E)(F)(G)(H)

And I have determined that I need to change the third position data from
'C' to a 'D', like this, it should now become:

(A)(B)(D)(D)(E)(F)(G)(H)

Now the third position is a 'D', and not a 'C'.

But this could be any data value.

I'm not familiar with file structures. But, if its a matter of defining
a structure of some sort that ALL files contain, I'm hopping that it will
be an easy step to set up a standard file structure (as long as I know
what the file containes. Could be an AVI file or a MPEG file or an TXT
file in most cases. So, I know that MPEG's have a certain file structure,
and AVI's do, but the TXT files I'm not sure of. Or, maybe it doesn't
matter, as long as I have the filename and size, I could write my one-byte
to (transpose) any data element that is at the user requested position.

Also, I'll assume that the file structures is the same for windows 98
and windows 2k and XP. I'm using XP and windows 98, if that matters.
And, I'm sure that others will be using XP mostly.

Lets see. There is FAT16, FAT32, NTFC

Please, I could really use the help on this. And it would save a lot
of time and file space if all I had to do was transpose 1 byte in a file,
like the above example.

Thanks really much.
-mydelphi

Comments

  • zibadianzibadian Member Posts: 6,349
    : ..but without reading and writing the whole file in question?
    :
    : Ok. Let me explain, if I can.
    :
    : Hello everyone. Its been a long time since I last singed on. Good
    : to be
    : back. Well, I have a question and I need your help.
    :
    : I'm trying to write to a file. Any file, since it doesn't matter at
    : this
    : point in time. But, because some of the times will be very large, I
    : do
    : not want to read/write in the triditional way, example:
    :
    : [code]:
    : repeat
    : BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
    : BlockWrite(ToF, Buf, NumRead, NumWritten);
    : until (NumRead = 0) or (NumWritten <> NumRead);
    : [/code]:
    :
    : What I want to do is just 'write' directly to the file itself. You
    : see,
    : the size is 1 byte (a number value or something else) and it only
    : needs
    : to be written in one place, but the place (position) will ber user
    : configurable, because the position will vary from file to file. But
    : they
    : only need to write one piece of data to the file. I'm using a
    : TrackBar
    : control and an EditBox for the file position edits.
    :
    : Sort of like this. Say you have a file who is only 8 bytes in size,
    : and
    : the data in it is obviously 8 pieces of data. Can be anything, but
    : lets
    : just simplify the example with a string of values who are
    : charactors, like
    : this:
    :
    : (A)(B)(C)(D)(E)(F)(G)(H)
    :
    : And I have determined that I need to change the third position data
    : from
    : 'C' to a 'D', like this, it should now become:
    :
    : (A)(B)(D)(D)(E)(F)(G)(H)
    :
    : Now the third position is a 'D', and not a 'C'.
    :
    : But this could be any data value.
    :
    : I'm not familiar with file structures. But, if its a matter of
    : defining
    : a structure of some sort that ALL files contain, I'm hopping that it
    : will
    : be an easy step to set up a standard file structure (as long as I
    : know
    : what the file containes. Could be an AVI file or a MPEG file or an
    : TXT
    : file in most cases. So, I know that MPEG's have a certain file
    : structure,
    : and AVI's do, but the TXT files I'm not sure of. Or, maybe it doesn't
    : matter, as long as I have the filename and size, I could write my
    : one-byte
    : to (transpose) any data element that is at the user requested
    : position.
    :
    : Also, I'll assume that the file structures is the same for windows 98
    : and windows 2k and XP. I'm using XP and windows 98, if that matters.
    : And, I'm sure that others will be using XP mostly.
    :
    : Lets see. There is FAT16, FAT32, NTFC
    :
    : Please, I could really use the help on this. And it would save a lot
    : of time and file space if all I had to do was transpose 1 byte in a
    : file,
    : like the above example.
    :
    : Thanks really much.
    : -mydelphi

    Just open an untyped file, then use seek() to move the "file cursor" to the location you need to change, and write the value you want:
    [code]
    AssignFile(f, 'myfile');
    Reset(f, 1);
    Seek(f, 500000000); // move to 500MB marker
    BlockWrite(f, 'A', 1); // place an 'A'
    CloseFile(f);
    [/code]
    As you can see frome the code, nothing is known about the file "myfile", except that it is at least 500MB large.
    Wether the drive is FAT16, FAT32, NTFS, CDFS, HFS+, FAT12, etc. is irrelevant for all file access, because you're not directly accessing the disk, but routing the call through the OS. Let the OS worry about the file system.
Sign In or Register to comment.