Important alert: (current site time 7/16/2013 5:13:42 AM EDT)
|
|
A more professional approach to file reading / writing
|
Email
|
| Submitted on: |
6/4/2004 3:46:55 PM |
| By: |
Olfert Bakker
|
| Level: |
Advanced |
| User Rating: |
By 2 Users |
| Compatibility: |
Delphi 7, Delphi 6, Delphi 5 |
| Views: |
15387 |
|
(About the author) |
|
|
|
I recieved a question about a more professional approach to file access, so I published the answer here. This article is not too usefull for people who have little experience, as I am assuming that you know AT LEAST the basics of Delphi 5+ in this article.
|
| | Terms of Agreement:
By using this article, you agree to the following terms...
- You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
- You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
- You may link to this article from another website, but ONLY if it is not wrapped in a frame.
- You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
|
For a somewhat mor professional approach to file reading and writing you might want to use the options of drag 'n' drop and windows' own shell possibilities. In this example I will be assuming that we are working with a text - type input, but the techniques described here are of course not limited to that. I'll assume that you want to make use of the file reading and writing options as used in most commercial software, which means using windows itself, which is rather tricky. For that, I am still looking for a way to do this without having to change the method for every windows version there is. I'll update the article when I'm finished with that.
First of all, let's take a look at the first option, creating your own file type. As you will be using this type for text (ANSI, ASCII) typed files only, you'll want to consider using the normal TextFile type, because working with typed files can be rather tricky and is usually not too efficient in RAM usage. If you really want to create your own filetype, I'd recommend a file consisting of lines or single characters, as the usage of string type variables will limit you to 255 characters. This way you could rather easily create tour own filetype by creating a record type to create e.g. 1 line in your file, or maybe a record containing a string type variable. The latter one isn't the most elegant way of solving the problem, but it is the easiest, so I'll give you an example here:
type
Line = record
Line:String[255];
end;
TTFile = file of Line;
Now, when you want to, say, save a file of this type, you'd have a procedure like this, assuming you have a form with a TMemo, TSaveDialog and a TButton:
Procedure Form1.Button1Click(Sender: TObject);
var
TTFile:file of Line;
i:integer;
begin
if SaveDialog1.Execute then begin
AssignFile(TTFile, SaveDialog1.FileName);
Rewrite(TTFile);
for i := 0 to Memo1.Lines.Count do begin
Line.Line := Memo1.lines[i];
Write(TTFile, Line);
end;
end;
CloseFile(TTFile);
end;
As for the icon and the automatic opening of your filetype: These are not controlled by your program, but by windows. There is a registry entry for each filetype in the windows registry, telling windows what icon is linked to what extension and what to do when a user wants to open that specific type of file. I could explain it to you here, but I can't really say I know the windows registry by heart, so I recommend you search the microsoft knowledge base for that, or maybe try using the TechNet website.
By the way: If you want to use the '.txt' extension but link it to your own program and icon, that is quite possible through the windows registry, though I doubt mr. Gates would appreciate you publishing your program after that ;-)
Now for drag and drop: This should be fairly easy for someone with experience. If you make one component (e.g. a TMemo or RichTextBox) a drop target, all you'd have to do is write some code that checks wether or not the file you're trying to drop is of the right type and open it, which should'nt be to hard. Do be carefull when working with a Multiple Document Interface (MDI), as it can sometimes produce pretty nasty errors when dragging and dropping out of another application (or from a folder).
I hope this will help you in working with files on a higher level. If you find this article a bit too much, you might want to try the article I wrote on file access earlier, which is more suited for beginners. You'll find it at http://www.planet-source-code.com/vb/default.asp?lngCId=1374&lngWId=7 If you have any questions or comments, I'll be glad to help, just email me.
BTW, I'll post a formatted version of this in a .zip (HTML).
|
| Other 2 submission(s) by this author
| |
Report Bad Submission
|
Your Vote
|
| |
Other User Comments
|
6/7/2004 5:17:16 AM: David Ward
Hi,
It would be useful for me, in considering the content of your article, if I knew exactly what the question you received was, perhaps you would like to post that too?
Cheers,
David.
(If this comment was disrespectful, please report it.)
| 8/27/2004 6:14:17 AM:
I have read your article I want get the article emailed to me
thanks (If this comment was disrespectful, please report it.)
| 9/15/2004 8:29:26 PM:
Please review this file (If this comment was disrespectful, please report it.)
| 11/16/2004 5:35:02 PM:
i want to be using ttree node and ttree nodes .i must be writting example programming.my book.i use panel and trichedit.please help me. (If this comment was disrespectful, please report it.)
| 1/23/2005 7:23:12 AM:
I'm trying to make a client server application. I use python for server, and delphi for client. Right now, i have a problem with writing a file. The server, read a file as binary, encode it with base64 and then send it to the client. The client accept it, read it as Raw String, and then decode it. THe problem is, how to write those string to a file? Can anyone help me about the code?
(If this comment was disrespectful, please report it.)
| 9/18/2008 11:33:54 AM: Branko
Please send me this file. Thanks, Branko (If this comment was disrespectful, please report it.)
| 1/6/2009 2:13:30 PM: Sam
I have read your article I want get the article emailed to me
thanks
Sam (If this comment was disrespectful, please report it.)
|
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)
To post feedback, first please login.
|
|