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

x
?
Solved

Reading and writing non ascii data from/to file

Posted on 2005-05-09
7
Medium Priority
?
344 Views
Last Modified: 2010-04-05
I have a globaly registered var f:file; I tried using blockread blockwrite but that didnt work too well (data got lost etc) I need 2 functions one that would take (var n : integer) and read that amount of bytes from file F and another that would take (var n: integer, TiffFile : string) that would read n bytes and write it to the tifffile

Any samples etc would be great.
Tom
0
Comment
Question by:T0masz
  • 4
  • 3
7 Comments
 

Author Comment

by:T0masz
ID: 13964840
procedure TForm1.Button1Click(Sender: TObject);
var
bry : TFileStream;
a : string;
begin

bry.Create('c:\test1.tif',fmOpenRead);
bry.Read(a,5);
ShowMessage(a);
end;
first try and it doesnt work...
0
 

Author Comment

by:T0masz
ID: 13964926
procedure TForm1.Button1Click(Sender: TObject);
var
bry : TFileStream;
a : string;
begin
bry.Create('c:\test1.tif',fmOpenRead);
bry.ReadBuffer(a,1);
ShowMessage(a);

end;
neither does this: stream read error
0
 
LVL 35

Expert Comment

by:Slick812
ID: 13965218
you have asked similar questions like this here before?
first, your string variable  a  has NO MEMORY when you try to write to it, you will need to first get it to have the correct amount of memory, by using the SetLength( ) on it.
next, you can not use ReadBuffer on a string variable, the string variable is just a pointer to the locaton of the string data



 procedure TForm1.Button1Click(Sender: TObject);
var
bry : TFileStream;
a : string;
size: Integer;
begin
bry.Create('c:\test1.tif',fmOpenRead);
size := bry.size;
SetLength(a, size);
bry.ReadBuffer(a[1],size);
FreeAndNil(bry);
{now the string   a  has all of the Tif  file in it, but it is usless to use a
ShowMessage(a)
for a string that has non-ascii data in it}
end;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:Slick812
ID: 13965248
here are two procedures that use strings, yes they are strings with just text in them, but it shows the methods of writting and reading the delphi String to and from a TFileStream, you can use both of these methods with Non-ASCII data,  and get the files into and out of the strings, but you can NOT use any string operations or functions on strings with #0 (non-ascii) bytes in them

procedure WriteToFile2;
var
MyString: String;
FileStream1: TFileStream;
begin
MyString := 'this is a string with several words '+
            'in it to be in a file';
FileStream1 := TFileStream.Create('G:\MyString.str',
                 fmCreate or fmOpenWrite or fmShareDenyWrite);
try
  FileStream1.WriteBuffer(MyString[1], Length(MyString));
  finally
  FileStream1.Free;
  end;
end;


procedure ReadFromFile2;
  var
  FileStream1: TFileStream;
  MyString: String;
begin
FileStream1 := TFileStream.Create('G:\MyString.str',
                 fmOpenRead or fmShareDenyWrite);
try
  if FileStream1.Size = 0 then Exit;
  SetLength(MyString, FileStream1.Size);
  FileStream1.ReadBuffer(MyString[1], FileStream1.Size);
  finally
  FileStream1.Free;;
  end;
ShowMessage(MyString);
end;
 

ther is some useful information about using TFileStreams at this web page -
http://www.angelfire.com/hi5/delphizeus/customfiles.html
0
 
LVL 35

Expert Comment

by:Slick812
ID: 13965280
if you are working with Non-ASCII data, like a TIFF image file, I would not think you would need a Text container like a Delphi String, you may could just use a Pointer


procedure TForm1.Button1Click(Sender: TObject);
var
bry : TFileStream;
p1: Pointer;
size: Integer;
begin
bry.Create('c:\test1.tif',fmOpenRead);
size := bry.size;
GetMem(p1, size);
bry.ReadBuffer(p1^,size);
FreeAndNil(bry);
{now the p1 memory block  has all of the Tif  file in it,
and you can write this memory block to a file}
bry := TFileStream.Create('c:\new1.tif',
            fmCreate or fmOpenWrite or fmShareDenyWrite);
bry.WriteBuffer(p1^, size);
FreeAndNil(bry);
FreeMem(p1);
end;
0
 

Author Comment

by:T0masz
ID: 13974494
procedure TForm1.Button1Click(Sender: TObject);
var
bry : TFileStream;
a : string;
size: Integer;
begin
bry.Create('c:\test1.tif',fmOpenRead);
size := bry.size;
SetLength(a, size);
bry.ReadBuffer(a[1],size);
FreeAndNil(bry);
{now the string   a  has all of the Tif  file in it, but it is usless to use a
ShowMessage(a)
for a string that has non-ascii data in it}
end;

this shrows a eaccessviolation at size := bry.size;

Tom
0
 
LVL 35

Accepted Solution

by:
Slick812 earned 2000 total points
ID: 13974823
procedure TForm1.Button1Click(Sender: TObject);
  var
  bry: TFileStream;
  MyString: String;
begin
bry := TFileStream.Create('c:\test1.tif',
                 fmOpenRead or fmShareDenyWrite);
try
  if bry.Size = 0 then Exit;
  SetLength(MyString,  bry.Size);
   bry.ReadBuffer(MyString[1],  bry.Size);
  finally
   bry.Free;;
  end;
ShowMessage(MyString);
end;

??
I'm a little bit wondering, this does not seem very difficult to me? the code above is the same code I already posted before.
What is it that you do not understand? ? maybe I can explain it to you somehow. You could read the Delphi Help for the TFileStream and read about the Create method and the Size property and the ReadBuffer Property for some information about these things

as to the -  accessviolation at size := bry.size;
how can you get an access violation for one integer equal to another integer? I would guess that you get the access violation at -
bry.Create('c:\test1.tif',fmOpenRead);

it just stops the code progression at - accessviolation at size := bry.size;

anyway the line -
 bry.Create('c:\test1.tif',fmOpenRead);

should be

bry := TFileStream.Create('c:\test1.tif',fmOpenRead);

I'm sorry I missed that, but it was in the other examples I gave?
by the way you can NEVER use the code for creating an object by calling the variable's create method, like
variable1.Create; // always throws an access violation

 you must Always use the code syntax of -

variable1 := TObjectType.Create;
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question