Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 729
  • Last Modified:

How To Open A File?

Hi there,

I am new to Delphi and hasn't been three weeks since I started learning it. I am currently involved in a program in which I have to open a file and read certain bytes out of it one at a time. Like first, 78 bytes and then 100 bytes and so on till the end of file.

But the problem is I don't know much about the syntaxes used to open files. I tried using help and used Assign method. But the problem is when I open the file and use the showmessage it only shows the first two characters and the rest are all blank. Like spaces. But they are not blank. They do contain characters.

Is it the way I am opening the file becuase someone said that I might have to open the file in binary mode or something to fully get al the characters exactly. So can you experts help me out in this situation...

The following is the code that I have been trying to use. And the type of file that I am reading is a file that comes from an equipment. Hope you guys can help me out and kind of explain the amendments made too.

unit StatFileDecoder;

interface

uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs, StdCtrls, Buttons, OleServer, CmAdmCtl, NMUUE;

type
   TMain = class(TForm)
    cmd_Process: TBitBtn;
    OD1: TOpenDialog;
    procedure cmd_ProcessClick(Sender: TObject);
    procedure FileProcess(FileName: String);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
   Main: TMain;

implementation

{$R *.dfm}

procedure TMain.cmd_ProcessClick(Sender: TObject);
var
   FileNumber: Integer;
begin
   If OD1.Execute then
   begin
      with OD1.Files do
      for FileNumber:= 0 to Count  -1 do
         FileProcess(Strings[FileNumber]);

   end;
end;

procedure TMain.FileProcess(FileName: String);
var
   File_Content: String;
   File_Handle: TextFile;
begin
   AssignFile(File_Handle,FileName);
   Reset(File_Handle);
   Readln(File_Handle,File_Content);
   showmessage(Copy(File_Content,5,5));
   CloseFile(File_Handle);
end;

end.

0
warheat001
Asked:
warheat001
1 Solution
 
Stuart_JohnsonCommented:
The way I'd do it is with FileStreams.  You can open the file in a stream, then seek to the position you need, and read X characters.  For instance:

procedure ReadFile(Filename: String; CharsToCopy: Integer; ReadPosition: Word);
var
  FS: TFileStream;
  Buff: Array of Char;
begin
  FS := TFileStream.Create(Filename, fmOpenRead);
  try
    FS.Seek(ReadPosition, soFromBeginning);
    SetLength(Buff, CharsToCopy);
    FS.Read(Buff, CharsToCopy);
  finally
    FS.Free;
  end;
end;

I haven't tested the above code, but it should work.

If you need any further help, please feel free to ask.

Stu.
0
 
snehanshuCommented:
warheat001,
  Your code would work if the file is a text file. If it is a binary file, you would need to do one of the following:
1) Define an appropriate record and read it record-wise.
2) Read byte by byte

For (1), I need to know the exact file structure, and here's the solution for (2):
The code reads the entire file into a dynamic array of bytes, "MyContents".
You can do whatever processing you want with this array once its read.
Hope this helps. Let me know if you need any clarifications.
...Snehanshu

procedure TForm1.Button2Click(Sender: TObject);
Var
  MyFile : File of Byte;
  MyFileName, MyStr : String;
  MyByte: Byte;
  MyContents: Array of byte;
begin
  //give your file name here or pass it as a parameter
  MyFileName := 'c:\abc.txt';
  setlength(MyContents, 0);
  If FileExists(MyFileName) Then
  Begin
    try
    Begin
      AssignFile(MyFile, MyFileName);
      Reset(MyFile);
      MyStr := '';
      While Not EOF(MyFile) Do
      Begin
       Read(MyFile, MyByte);
       //Just to show some output
       MyStr := MyStr + intToStr(MyByte)+',';

       //Dynamically increment length of the array
       setlength(MyContents, Length(MyContents)+1);
       MyContents[High(MyContents)] := MyByte;

      End;
      CloseFile(MyFile);
      ShowMessage(MyStr);
//write code to process MyContents here

//Free array after processing
      setlength(MyContents, 0);
    end
    except on e:Exception do
    begin
      Raise e;
    end;
    end;

  End
  Else
    ShowMessage('File does not exist');

end;
0
 
snehanshuCommented:
Here's a slightly modified code that shows how to process the array :)

...Shu

procedure TForm1.Button2Click(Sender: TObject);
Var
  MyFile : File of Byte;
  MyFileName, MyStr : String;
  MyByte: Byte;
  MyContents: Array of byte;
  MyCtr: Integer;
begin
  //give your file name here or pass it as a parameter
  MyFileName := 'c:\abc.txt';
  setlength(MyContents, 0);
  If FileExists(MyFileName) Then
  Begin
    try
    Begin
      AssignFile(MyFile, MyFileName);
      Reset(MyFile);
      MyStr := '';
      While Not EOF(MyFile) Do
      Begin
        Read(MyFile, MyByte);

        //Dynamically increment length of the array
        setlength(MyContents, Length(MyContents)+1);
        MyContents[High(MyContents)] := MyByte;

      End;
      CloseFile(MyFile);

//write code to process MyContents here
//here's come sample code to show how to access the array
      For MyCtr := low(MyContents) to High(MyContents) Do
        MyStr := MyStr + IntToStr(MyContents[MyCtr])+',';

      ShowMessage(MyStr);
//Free array after processing
      setlength(MyContents, 0);
    end
    except on e:Exception do
    begin
      Raise e;
    end;
    end;

  End
  Else
    ShowMessage('File does not exist');

end;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
warheat001Author Commented:
Thankyou very much  Stuart Johnson for your quick reply.

But could you tell me how I can put that <FS.Read(Buff, CharsToCopy);> into a variable or how I can read it into a variable or put into a database. Just the putting part only.

Thankyou Very Much

0
 
Stuart_JohnsonCommented:
Hi warheat001,

After testing the code I posted above, it appears it doesn't work with dynamic arrays.  Unfortunately I've just run short of time and I can't code anything at the moment for you, but I will definately do something for you ASAP.

Sorry not to be able to help immediately.

Stuart.
0
 
BJThomsenCommented:
procedure ReadFromFile;
  var
    FileStream: TFileStream;
    MyString: String;
begin
  FileStream := TFileStream.Create(<filename>,  fmOpenRead or fmShareDenyWrite);
  try
    SetLength(MyString,<length you want to read>);
    FileStream1.Read(MyString[1], <length you want to read>);
  finally
    FileStream.Free;
  end;
  ShowMessage(MyString);
end;
 
0
 
swift99Commented:
I would use Blockread, which is what the TFileStream does "under the .hood"
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now