| |

VerySource

 Forgot password?
 Register
Search
View: 919|Reply: 5

A little question about file streaming!

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-3 11:10:01
| Show all posts |Read mode
type
  TStorage = packed record
    bt01: byte;
    sName: string [14];
    SaveDate: TDateTime;
    dwMakeIdx: DWord;
    wIndex: Word;
    wDura: Word;
    wDuraMax: Word;
    nValue: array [0..13] of Byte;
    nNone: Integer; // Reserved bytes ???
  end;

procedure TForm1.btn1Click (Sender: TObject);
var
  Stor: TStorage;
  I, nCount: Integer;
  Files: TFileStream;
begin
  Files: = TFileStream.Create (Pchar (edt1.text), fmOpenReadWrite or fmShareDenyNone);
  Files.Read (nCount, SizeOf (Integer));
  for I: = 1 to nCount do
  begin
    Files.Read (Stor, SizeOf (TStorage));
    Files.Seek (I * SizeOf (TStorage), 3);
    mmo1.Lines.Add (IntToStr (Stor.wIndex-1));
  end;
end;

The code above is why the data read when the second parameter of Seek is equal to 0, 1 or 2 is not correct. It is correct when it is equal to 3 or larger numbers. This is not only defined 0, 1 and 2 Are there three methods of operation?
Reply

Use magic Report

0

Threads

15

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-3 14:06:01
| Show all posts
Your code is wrong. Seek is not necessary in your code, you don't have to. If you have to write, you also have to do the following:

  for I: = 1 to nCount do
  begin
    Files.Seek (I * SizeOf (TStorage), 0);
    Files.Read (Stor, SizeOf (TStorage));
    mmo1.Lines.Add (IntToStr (Stor.wIndex-1));
  end;

0 starts from the file, 1 starts from the current, and 2 starts from the end of the file.
Reply

Use magic Report

0

Threads

15

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-3 14:09:01
| Show all posts
No, I didn't find that your loop variable starts at 1:
  for I: = 0 to nCount-1 do
  begin
    Files.Seek (I * SizeOf (TStorage), 0);
    Files.Read (Stor, SizeOf (TStorage));
    mmo1.Lines.Add (IntToStr (Stor.wIndex-1));
  end;
or:
  for I: = 1 to nCount do
  begin
    Files.Seek ((I-1) * SizeOf (TStorage), 0);
    Files.Read (Stor, SizeOf (TStorage));
    mmo1.Lines.Add (IntToStr (Stor.wIndex-1));
  end;
Reply

Use magic Report

0

Threads

15

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-3 14:21:01
| Show all posts
Is it right when it is equal to 3 or more?
======================================================== =========================
This happened to be correct. Because your loop variable starts from 1, and your Seek and Read order is reversed. When the second parameter of the Seek function is> 2, it is automatically corrected to 0, so Seek is exactly equal to the position of the next record. Right.
Reply

Use magic Report

0

Threads

53

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-1-5 07:30:01
| Show all posts
njy7648is everywhere. It's not easy. Come to my company Ha ha But I'm afraid I can't afford you
Reply

Use magic Report

0

Threads

15

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-5 09:00:01
| Show all posts
Everyone has a rest today, so I am here to make my doubts, haha, I really come to your company. I'm afraid not to pay, and your company will not.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list