26278 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Nintendo throws
flaming legal
barrel at YouTubing
fans
Optus outlines its
4G future
Hold our tiny
silicon spheres,
say gravity wave
detection
scientists
EMC vuln gives mere
sysadmins the power
of storage admins
Four Anons cuffed
in Italy
IBM gives a cloudy
outlook for COBOL
Bureau of Stats
releases
educational
SimClone game
I know who "Satoshi
Nakamoto" is, says
Ted Nelson
Google builds
crowdsourcing into
new Maps code stack
Google"s Native
Code browser tech
goes cross-platform
Slashdot
Ask Slashdot:
Wiring Home
Furniture?
Medical Firm Sues
IRS For 4th
Amendment Violation
In Records Seizure
Military Dolphins
Discover 1800s
Torpedo
Apple Mobile
Devices Cleared For
Use On US Military
Networks
Mice, Newts
Retrieved After a
Month Orbiting
Earth At 345 Miles
Up
IBM Takes System/z
To the Cloud With
COBOL Update
Google"s Nexus Q
Successor Hits the
FCC
Yahoo Board
Approves a $1.1B
Pricetag For Tumblr
Trade Group: US
Software Developer
Wages Fell 2% Last
Year
Wikileaks Releases
Docs Before Trial
of TPB Founder Warg
Article viewer

Managing Filestreams



Written by:dimport
Published by:Nightscript
Published on:2003-06-21 07:19:46
Topic:Delphi
Search OSI about Delphi.More articles by dimport.
 viewed 12318 times send this article printer friendly

Digg this!
    Rate this article :
To read from and write to a file the best method in Delphi would probably be the TFilestream component. I will go through the basics of TFilestream. This tutorial assumes some basic knowledge in Delphi or Pascal.

Remember to add Classes to your uses clause to use Tfilestream.
Creation:
[i]
var
FileS: TFilestream;
begin
FileS := TFilestream.Create('C: estfile.txt',fmOpenRead,fmShareDenyWrite);
try
  {code goes here}
finally
  FileS.Free;
  end;
end;
[/i]


This shows you how to create a Filestream. The first parameter defines the filename. Here i have just used a sample text file. The next parameter is the file mode. Where I have opened for reading only. The possible values are:

fmCreate - Create a file, open in write mode if exists.

fmOpenRead - Open for just reading

fmOpenWrite - Open for just

fmOpenReadWrite - Open for both reading and writing.

A third parameter is also possible to add, although you do not have to add it. This is the Share mode parameter. It tells what right other applications have to use the same file you are using. The possible values are:

fmShareCompat - Sharing is compatible with the way FCBs are opened.fmShareExclusive - Other apps may not open this file.

fmShareDenyWrite - Other apps may not write to this file.

fmShareDenyRead - Other apps may not Read from this file

fmShareDenyNone - The file is open for any use from other apps.

you should always use try .. finally .. end; to make sure that your Filestream is freed, no matter what happens.

Reading:
 [i]var
FileS: TFilestream;
I: Integer;
Buff: Byte;
Teststring: String;
begin
FileS := TFilestream.Create('C: estfile.txt',fmOpenRead);
try
  for I := 0 to FileS.Size do
    begin
    FileS.Read(Buff,1);
    Teststring := Teststring + Chr(Buff);
    end;
finally
  FileS.Free;
  end;
end;
[/i]


This shows a basic usage of the filestream. It goes through every byte in the textfile, and appends them to a string. Although this doesn't do anything it shows how to read. The first parameter tells the filestream what buffer to read to. Here we have used a single byte. The second parameter tells the filestream how many bytes to read. Here we have to use 1 because we're only reading one byte at a time.If you want to read several bytes at a time you have to use an array of bytes.

Writing:
 [i]var
FileS: TFilestream;
I: Integer;
Buff: Byte;
begin
FileS := TFilestream.Create('C: estfile.txt',fmCreate);
try
  for I := 0 to 299 do
    begin
    Byte := Random(255);
    FileS.Write(Buff,1);
    end;
finally
  FileS.Free;
  end;
end;
[/i]


This example shows how to write to a file. It creates C: estfile.txt and writes 300 characters of random information. The first parameter in the Write function is the buffer to write from. The second tells how many bytes to write. And as in reading, to write several bytes at a time you have to use an array of bytes.

I have given you a few examples for using TFilestream. This should hold for a lot of tasks involving files. You don't have to use Byte, you can also use other buffer types. String, Integer and so on (I haven't tried this yet though). Remember that the Write and Read functions return how many bytes that have actually been read, as it isn't always possible to read the amount you have requested in the second parameter. For more information and definitions, consult the Delphi help file.

SkyFex (skyfex@hotmail.com)

This article was originally written by skyfex

Did you like this article? There are hundreds more.

Comments:
cassius
2004-05-22 12:32:02
This article is just what I needed! I was stuck at GEEK 5, because I was using the ordinary file handling procedures. Thanks!
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:



     
Your Ad Here
 
Copyright Open Source Institute, 2006