Solved

How can i save resource in a .dat file?

Posted on 2011-03-20
34
1,389 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:WhiteSeed
  • 12
  • 12
  • 6
  • +1
34 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35175031
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
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35175039
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
 
LVL 24

Expert Comment

by:jimyX
ID: 35175073
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
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35175090
ok, i've understand, but you can add a simple example please?
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35175109
What Delphi version you are using?
0
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35175431
i use now delphi XE
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35175479
OK I am working on it.
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35175599

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
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35175636
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
 
LVL 32

Expert Comment

by:ewangoya
ID: 35180745

I'll give you an example later in the day
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 35180908
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 35181008
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
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35181061
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 35181089
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
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35181192
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
 
LVL 32

Expert Comment

by:ewangoya
ID: 35181224

Unless you are creating your own custom database
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35181275
Or you can use DLL files. Windows OS is full of dll's that MS uses to store most of the resources.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 24

Expert Comment

by:jimyX
ID: 35181461
BTW, I created the App+DB that stores images in it (simple demo).
0
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35181464
is exactly what I want to create, my custom database. i use the dll to reduce from the side of the program code
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35181695
Which direction you are going, DB or DLL?
0
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35181697
if you add the demo jimyX do me a favor :)
0
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35181703
database, or custom database
0
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 150 total points
ID: 35182468
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
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35182637
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
 
LVL 24

Expert Comment

by:jimyX
ID: 35183269
I recommend the DLL option where all your resources are saved in DLL files.
0
 
LVL 4

Author Comment

by:WhiteSeed
ID: 35183384
if you have ready an example with ddl and you can post it you do me a favor
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35183542
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
 
LVL 32

Expert Comment

by:ewangoya
ID: 35184388

Hey JimmyX

What if you want to add a member with new images? DLL will not work.
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 350 total points
ID: 35186697

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
 
LVL 24

Expert Comment

by:jimyX
ID: 35186733
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 35187031
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
 
LVL 32

Expert Comment

by:ewangoya
ID: 35189518

@jimmyX
Its hard to please the authors these days LOL
0
 
LVL 4

Author Closing Comment

by:WhiteSeed
ID: 35190954
thx at all, i hope to find the right metod
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35205375
@ 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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
A short article about problems I had with the new location API and permissions in Marshmallow
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now