#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Create/save "empty" file of specific size


    This is probably a noob question, but I want to create/save a file of bytes of a specific size -- 1521 bytes to be exact -- and write in variables in specific places, specifically 4 integers into indexes 41, 44, and 1505, 1506. For some reason my brain just does not work at the moment. Even a low-level Pascal AssignFile operation suffices. I just cannot wrap my mind around the specific size thing.

    Yeah, so the questions is how to do such a thing?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    Does this do what you are asking for?
    Code:
    TForm1 = class(TForm)
     ...
    Private
       A: Array[0..1520] of Byte;
    end;
    
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
       MS: TMemoryStream;
       I: Integer;
    begin
       MS := TMemoryStream.Create;
       try
          FillChar(A, SizeOf(A), 0);
          //fill the indexes you mentioned in your post with values
          //lowest value possible 0 highest value possible 255 
          //which is the range for a byte.
          A[41] := 20;
          A[44] := 120;
          A[1505] := 255;
          A[1506] := 0;
          //show values are in the array
          ShowMessage('Index 41 = '+IntToStr(A[41])+slineBreak+
                      'Index 44 = '+IntToStr(A[44])+slineBreak+
                      'Index 1505 = '+IntToStr(A[1505])+slineBreak+
                      'Index 1506 = '+IntToStr(A[1506]));
          for I := Low(A) to High(A) do
             MS.Write(A[I], SizeOf(A[I]));
          //save data to a file
          MS.SaveToFile('D:\Empty.sve');
       finally
          MS.Free;
       end;
       //clear array just to show it no longer holds values.
       FillChar(A, SizeOf(A), 0);
       ShowMessage('Index 41 = '+IntToStr(A[41])+slineBreak+
                      'Index 44 = '+IntToStr(A[44])+slineBreak+
                      'Index 1505 = '+IntToStr(A[1505])+slineBreak+
                      'Index 1506 = '+IntToStr(A[1506]));
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    var
       MS: TMemoryStream;
       I: Integer;
    begin
       MS := TMemoryStream.Create;
       try
          //read the data from the same file
          MS.LoadFromFile('D:\Empty.sve');
          MS.Position := 0;
          for I := Low(A) to High(A) do
             MS.Read(A[I], SizeOf(A[I]));
          ShowMessage('Index 41 = '+IntToStr(A[41])+slineBreak+
                      'Index 44 = '+IntToStr(A[44])+slineBreak+
                      'Index 1505 = '+IntToStr(A[1505])+slineBreak+
                      'Index 1506 = '+IntToStr(A[1506]));
       finally
          MS.Free;
       end;
    end;
    Attached Images
    Last edited by majlumbo; January 24th, 2014 at 09:42 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Red face


    Yeah, it does. For some unexplainable reason I didn't think of a MemoryStream. Cheers.

IMN logo majestic logo threadwatch logo seochat tools logo