Link to home
Start Free TrialLog in
Avatar of WhiteSeed
WhiteSeedFlag for Italy

asked on

How can i save resource in a .dat file?

hi, i've a record
type
   TMyRecord = record
     Name: string[50];
     Surname: char;
   end;
var Members: array[1..50] of TMyRecord; 

Open in new window

i use this code for insert the records in a .dat file:
var
   F : file of TMyRecord;
   i : integer;
 begin
  AssignFile(F,'members.dat') ;
  Rewrite(F) ;
  try
   for j:= 1 to 50 do
    Write (F, Members[j]) ;
  finally
   CloseFile(F) ;
  end;
 end;
 

Open in new window

and this code for read:
 var
   Member: TMyRecord
   F : file of TMyRecord;
 begin
  AssignFile(F,'members.dat') ;
  Reset(F) ;
  try
   while not Eof(F) do begin
    Read (F, Member) ;
    showmessage(member.name);
    showmessage(member.surname);
   end;
  finally
   CloseFile(F) ;
  end;
 end; 

Open in new window

How can i add and read image or other resource on this .dat file?
Avatar of jimyX
jimyX

Using dat file is limited you can't add resources freely you will be restricted to string format and you have to convert it to be able to handle it (it depends on the kind of resources you want to add though). How about using simple database instead, because saving the image in .dat will be in string format and will not be as visual Image.
Avatar of WhiteSeed

ASKER

I would release my application with one or more files that contains the resources decryptable only from my application  so as not to complicate the exe, How can i do it?
That's why I mentioned simple database. It's going to be a very simple database which does not require any complication (no driver and no special configuration when distributed), you just put with your exe and start using same idea like the dat file but the difference is, it's more flexible, high functionality and easy to use.
I repeat when distributing your application you will give one file along with your exe which is the database, and the users will just use your application without doing any extra process whatsoever to handle the database.
ok, i've understand, but you can add a simple example please?
What Delphi version you are using?
i use now delphi XE
OK I am working on it.

This all depends an what kind of data you will be storing in the .dat file

If you are storing a large number of member records or you expect the records to grow over time, then forget the .dat file, you need a proper database, and I don't just mean a simple or embedded database. There are many free databases out there that you can use and I would recommend SQL Server Express. You can use dbGO components, thats the simplest path to take.

When it comes to images, very important, it is never a good idea to store your images in the database to begin with. It becomes a backup and access issue. Depending on your database, accessing images from the db can be frustratingly sluggish. I have had to fix a lot of such applications in my time.

If you are thinking of a very few images just for your GUI design, this can be comfortably stored in a .dat file with no problem
If you are going to store a lot of images maybe corresponding to the members, this need to be in some folder on your system which will be referenced in your database
eg
MemberTable  
    MemberID, MemberName, other fields

MemberDocuments
   MemberID, DocumentType, DocumentPath (This will reference a path on your system)


Hope that helps you a bit

thx for the answer I want to understand this concept better. Suppose that all the graphics (background, cursor, etc ...) should be included in a file. dat (or. personal) to avoid encumbering the executable how I should  do it? of course i need to write and read the resources.
Sorry for my english

I'll give you an example later in the day
in a file ?
use a database like indicated by jimyX and ewangoya

from what i see ... those code samples come from a school text book ...
basic pascal reading/writing records to/from file

if this a school exercise ?
fwiw ...
you could also use streams like in the good ol days of turbo pascal 6.0


pseudo code:
 
procedure SaveRec(FileName: string; Name, SurName: string; Image: TBitmap);
begin 
  f := TFileStream.Create('c:\temp\test.dat');
  try
    f.Write(Lenth(Name), SizeOf(Integer));
    f.Write(Name, Length(Name));
    f.Write(Length(SurName), SizeOf(Integer));
    f.Write(SurName, Length(SurName));
    Image.SaveToStream(f);
  finally
    f.Free;
  end;
end;

Open in new window


but this code is at least 20 years old (and i didn't test it ...)
no, this code is a little mod from about.com
I am creating an application that requires a lot of images to which each is associated with a set of data( width, height for example). I can not put all in the executable because it would become too heavy, but I plan to use a file (like most applications do) which images and their associated data and the Load event load this data in the application.
if my solution is feasible, I will create a file. myapp and distribute this with my application.
if it's just for images
it would be a lot easier to use a compression tool like pkzip to add your image to the zip archive
extracting is then done with pkunzip

feasible ? probably not

most apps (probably 95%) use a database for something like this
some new databases have compression built in like oracle 11.2.0.2
mmm.. and can i make a database che only my application can open?. For example in some games there are many file with all the image and video and you can't open it with notepad, access, oracle, ecc...

Unless you are creating your own custom database
Or you can use DLL files. Windows OS is full of dll's that MS uses to store most of the resources.
BTW, I created the App+DB that stores images in it (simple demo).
is exactly what I want to create, my custom database. i use the dll to reduce from the side of the program code
Which direction you are going, DB or DLL?
if you add the demo jimyX do me a favor :)
database, or custom database
SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i've just try the example, i think is similar to using ado. Thx at all for the answer wait for end the ewangoya's example
I recommend the DLL option where all your resources are saved in DLL files.
if you have ready an example with ddl and you can post it you do me a favor
Here is an example, with full details, on how to create and use DLL of resources:
http://delphi.about.com/library/weekly/aa010405a.htm

Hey JimmyX

What if you want to add a member with new images? DLL will not work.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi ewangoya,
>  "What if you want to add a member with new images? DLL will not work."

I know. I already offered the Database method but the author was not interested in the DB option, that's why I suggested the DLL.
lol, nice idea ewangoya :)

WhiteSeed
you will always use a database
in the beginning there were only ISAM databases, which is 1 file per table
a TClientDataset is just another component to use ISAM files as a database

@jimmyX
Its hard to please the authors these days LOL
thx at all, i hope to find the right metod
@ WhiteSeed
>   "I hope to find the right method"
The right method depends on your current requirement or project at hand. We would have helped you better if your problem was clearly expressed or explained. Consider this for future questions please ;-)

@ ewangoya
Ya, pretty much hard. After all the time and effort, B grade, what a pity.
And on the other side, you got points twice as I got ;-)