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?
LVL 4
WhiteSeedAsked:
Who is Participating?
 
Ephraim WangoyaCommented:

Simple examples
I just used the extension .res for the files, you can use any extension you like



Unit3.dfm
Unit3.pas
images.res
members.res
0
 
jimyXCommented:
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.
0
 
WhiteSeedAuthor Commented:
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?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
jimyXCommented:
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.
0
 
WhiteSeedAuthor Commented:
ok, i've understand, but you can add a simple example please?
0
 
jimyXCommented:
What Delphi version you are using?
0
 
WhiteSeedAuthor Commented:
i use now delphi XE
0
 
jimyXCommented:
OK I am working on it.
0
 
Ephraim WangoyaCommented:

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

0
 
WhiteSeedAuthor Commented:
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
0
 
Ephraim WangoyaCommented:

I'll give you an example later in the day
0
 
Geert GOracle dbaCommented:
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 ?
0
 
Geert GOracle dbaCommented:
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 ...)
0
 
WhiteSeedAuthor Commented:
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.
0
 
Geert GOracle dbaCommented:
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
0
 
WhiteSeedAuthor Commented:
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...
0
 
Ephraim WangoyaCommented:

Unless you are creating your own custom database
0
 
jimyXCommented:
Or you can use DLL files. Windows OS is full of dll's that MS uses to store most of the resources.
0
 
jimyXCommented:
BTW, I created the App+DB that stores images in it (simple demo).
0
 
WhiteSeedAuthor Commented:
is exactly what I want to create, my custom database. i use the dll to reduce from the side of the program code
0
 
jimyXCommented:
Which direction you are going, DB or DLL?
0
 
WhiteSeedAuthor Commented:
if you add the demo jimyX do me a favor :)
0
 
WhiteSeedAuthor Commented:
database, or custom database
0
 
jimyXCommented:
Download and install Absolute DB (free trial and Personal Edition which can be used only for non-commercial purposes):
http://www.componentace.com/data/distr/absolute_d15_607.zip

Here is the demo attached.

PS: EE refused to upload one of the files so i removed it's extension. So when you download, just rename SSDB to SSDB.ABS
Simple-image-to-from-DB.zip
0
 
WhiteSeedAuthor Commented:
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
0
 
jimyXCommented:
I recommend the DLL option where all your resources are saved in DLL files.
0
 
WhiteSeedAuthor Commented:
if you have ready an example with ddl and you can post it you do me a favor
0
 
jimyXCommented:
Here is an example, with full details, on how to create and use DLL of resources:
http://delphi.about.com/library/weekly/aa010405a.htm
0
 
Ephraim WangoyaCommented:

Hey JimmyX

What if you want to add a member with new images? DLL will not work.
0
 
jimyXCommented:
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.
0
 
Geert GOracle dbaCommented:
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
0
 
Ephraim WangoyaCommented:

@jimmyX
Its hard to please the authors these days LOL
0
 
WhiteSeedAuthor Commented:
thx at all, i hope to find the right metod
0
 
jimyXCommented:
@ 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 ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.