Academic Tutorials



English | French | Portugese | German | Italian
Home Advertise Payments Recommended Websites Interview Questions FAQs
News Source Codes E-Books Downloads Jobs Web Hosting
Chats

Delphi
Introduction to Delphi
Data Types
Programming Logic
Case statements
Delphi-loops
Overview of Subroutine
Delphi Exception handling
Delphi Date Time
Delphi Filest
Delphi Binary files
Delphi Pointers
Delphi Printing text and Graphics

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
CSS 1.0
CSS 2.0
HLML
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
CVS
Python
Apple Script
PL/SQL Tutorial
SQL Server
PHP
.NET (dotnet)
Microsoft.Net
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
VC++
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Photoshop Tutorial
Gimp Tutorial
Matlab
Gnuplot Programming
GIF Animation Tutorial
Scientific Visualization Tutorial
Graphics
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Weblogic Tutorial
SEO
Web Site Hosting
Domain Name
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Eclipse
J2ME
JBOSS
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Cobol
Assembly Language
Mainframe
Forth Programming
Lisp Programming
Pascal
Delphi
Fortran
OOPs
Data Warehousing
CGI Programming
Emacs Tutorial
Gnome
ILU
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills
Database Tutorials
Oracle
MySQL
Operating System
BSD
Symbian
Unix
Internet
IP-Masquerading
IPC
MIDI
Software Testing
Testing
Firewalls
SAP Module
ERP
ABAP
Business Warehousing
SAP Basis
Material Management
Sales & Distribution
Human Resource
Netweaver
Customer Relationship Management
Production and Planning
Networking Programming
Corba Tutorial
Networking Tutorial
Microsoft Office
Microsoft Word
Microsoft Outlook
Microsoft PowerPoint
Microsoft Publisher
Microsoft Excel
Microsoft Front Page
Microsoft InfoPath
Microsoft Access
Accounting
Financial Accounting
Managerial Accounting
Network Sites


Delphi file support


Previoushome Next






Delphi file support

Delphi provides a number of different file access mechanisms. The oldest is in support of consoles, where the Read, ReadLn, Write and WriteLn routines have a syntax that omits the file name. With no file name, IO (Input and Output) is routed to the console.

A D V E R T I S E M E N T

Of greater importance to modern applications, are disk file operations. Disks such as hard disks, floppy disks, CDs and DVDs (the latter are treated as read only).
Delphi confusingly provides two basic sets of routines for file handling. The most Delphi like are covered by this article and this web site. The other type are thin wrappers around Windows APIs - and as such are platform specific. They also support text files less intuitively. They are not covered here.
Additionally, hidden away, Delphi provides a very elegant way of reading and writing complete text files. The TStringList class has methods for loading the list of strings from a text file. And for saving the list likewise. See the final section of this article.

Accessing files

There are a number of basic operations for handling both text and binary files. (The latter can hold non character data values). First, we must get a handle for a named file:

var
myFile : TextFile;

begin
AssignFile(myFile, 'Test.txt');

Here we are getting a handle to a text file, designated by the TextFile type (binary files are of type File). We ask Delphi to assign a file handle for a file called 'Test.txt' which will be assumed to be in the current directory (as given by the GetCurrentDir routine).
Next, we must open the file using this handle. This operation tells Delphi how we want to treat the file. There are 3 ways of opening the file:
ReWrite Opens a file as new - discards existing contents if file exists
Reset Opens a file for read and write access
Append Opens a file for appending to the end (such as a log file)

We'll cover the access mechanisms for text and binary files separately. Meanwhile, when we have finished, we must close the file:
CloseFile(myFile);


Reading and writing to text files

Text files are great for simple uses, such as where we record a processing log. Text files fall short when reading and writing structured data. They do support number to string and string to number formatting, but you are often better off defining your own record structure and using a typed binary file instead.
Here is a simple example of access to a text file:

var
myFile : TextFile;
text : string;

begin
// Try to open the Test.txt file for writing to
AssignFile(myFile, 'Test.txt');
ReWrite(myFile);

// Write a couple of well known words to this file
WriteLn(myFile, 'Hello');
WriteLn(myFile, 'World');

// Close the file
CloseFile(myFile);

// Reopen the file for reading
Reset(myFile);

// Display the file contents
while not Eof(myFile) do
begin
ReadLn(myFile, text);
ShowMessage(text);
end;

// Close the file for the last time
CloseFile(myFile);
end;

The ShowMessage routine displays the following :

Hello
World

If we replaced the ReWrite routine with Append, and rerun the code, the existing file would then contain:
Hello
World
Hello
World

since append retains the existing file contents, appending after this anything new written to it.
Notice that we have used WriteLn and ReadLn to write to and read from the file. This writes the given text plus a carriage return and line feed to the text. The read reads the whole line up to the carriage return. We have read from the file until Eof (End Of File) is true. See also Eoln.
We can use Read and Write to read and write multiple strings to a file. More importantly, we can use these to write numbers as strings, with some useful formatting for further details):
var
myFile : TextFile;
text : string;
i: Integer;

begin
// Try to open the Test.txt file for writing to
AssignFile(myFile, 'Test.txt');
ReWrite(myFile);

// Write a couple of well known words to this file
Write(myFile, 'Hello ');
Write(myFile, 'World');

// Terminate this line
WriteLn(myFile);

// Write some numbers to the file as a single line
for i := 2 to 4 do
Write(myFile, i/2, '');

// Terminate this line
WriteLn(myFile);

// repeat the above, but with number formatting
for i := 2 to 4 do
Write(myFile, i/2:5:1);

// Terminate this line
WriteLn(myFile);

// Close the file
CloseFile(myFile);

// Reopen the file for reading only
Reset(myFile);

// Display the file contents
while not Eof(myFile) do
begin
ReadLn(myFile, text);
ShowMessage(text);
end;

// Close the file for the last time
CloseFile(myFile);
end;

Hello World
1.00000000000000E+00001.50000000000000E+00002.00000000000000E+0000
1.01.52.0


Reading and writing to typed binary files

Typed binary files are files that have a data type as the basic unit of writing and reading. You write, say, an Integer, or aRecord to a file, and read the same unit of data back. Records are particularly useful, allowing us to store any mix of data types in the one file unit of data.
This is best illustrated with an example:

type
TCustomer = Record
name : string[20];
age: Integer;
male : Boolean;
end;

var
myFile : File of TCustomer;// A file of customer records
customer : TCustomer;// A customer record variable

begin
// Try to open the Test.cus binary file for writing to
AssignFile(myFile, 'Test.cus');
ReWrite(myFile);

// Write a couple of customer records to the file
customer.name := 'Fred Bloggs';
customer.age:= 21;
customer.male := true;
Write(myFile, customer);

customer.name := 'Jane Turner';
customer.age:= 45;
customer.male := false;
Write(myFile, customer);

// Close the file
CloseFile(myFile);

// Reopen the file in read only mode
FileMode := fmOpenRead;
Reset(myFile);

// Display the file contents
while not Eof(myFile) do
begin
Read(myFile, customer);
if customer.male
then ShowMessage('Man with name '+customer.name+
' is '+IntToStr(customer.age))
else ShowMessage('Lady with name '+customer.name+
' is '+IntToStr(customer.age));
end;

// Close the file for the last time
CloseFile(myFile);
end;

Man with name Fred Bloggs is 21
Lady with name Jane Turner is 45

The code is very similar to that used for text files, except that we define a file of a certain type (record), and pass/receive record data when writing/reading.

Be the first one to comment on this page.




  Delphi eBooks
More Links » »
 
 Delphi FAQs
More Links » »
 
 Delphi Interview Questions
More Links » »
 
 Delphi Articles

No Delphi Articles could be found as of now.

 
 Delphi News

No News on Delphi could be found as of now.

 
 Delphi Jobs

No Delphi Articles could be found as of now.


Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previoushome Next

Keywords: delphi pointers,delphi programming,delphi files,delphi datatypes,delphi text,delphi graphics,delphi records,delphi text printing

HTML Quizzes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
CSS 1.0 Quiz
CSS 2.0 Quiz
HLML Quiz
XML Quizzes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizzes
JavaScript Quiz
VBScript Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizzes
ASP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
CVS Quiz
Python Quiz
Apple Script Quiz
PL/SQL Quiz
SQL Server Quiz
PHP Quiz
.NET (dotnet) Quizzes
Microsoft.Net Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
VC++ Quiz
Multimedia Quizzes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Photoshop Quiz
Gimp Quiz
Matlab Quiz
Gnuplot Programming Quiz
GIF Animation Quiz
Scientific Visualization Quiz
Graphics Quiz
Web Building Quizzes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Weblogic Quiz
SEO Quiz
Web Site Hosting Quiz
Domain Name Quiz
Java Quizzes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Eclipse Quiz
J2ME Quiz
JBOSS Quiz
Programming Langauges Quizzes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Cobol Quiz
Assembly Language Quiz
Mainframe Quiz
Forth Programming Quiz
Lisp Programming Quiz
Pascal Quiz
Delphi Quiz
Fortran Quiz
OOPs Quiz
Data Warehousing Quiz
CGI Programming Quiz
Emacs Quiz
Gnome Quiz
ILU Quiz
Soft Skills Quizzes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz
Database Quizzes
Oracle Quiz
MySQL Quiz
Operating System Quizzes
BSD Quiz
Symbian Quiz
Unix Quiz
Internet Quiz
IP-Masquerading Quiz
IPC Quiz
MIDI Quiz
Software Testing Quizzes
Testing Quiz
Firewalls Quiz
SAP Module Quizzes
ERP Quiz
ABAP Quiz
Business Warehousing Quiz
SAP Basis Quiz
Material Management Quiz
Sales & Distribution Quiz
Human Resource Quiz
Netweaver Quiz
Customer Relationship Management Quiz
Production and Planning Quiz
Networking Programming Quizzes
Corba Quiz
Networking Quiz
Microsoft Office Quizzes
Microsoft Word Quiz
Microsoft Outlook Quiz
Microsoft PowerPoint Quiz
Microsoft Publisher Quiz
Microsoft Excel Quiz
Microsoft Front Page Quiz
Microsoft InfoPath Quiz
Microsoft Access Quiz
Accounting Quizzes
Financial Accounting Quiz
Managerial Accounting Quiz
Testimonials | Contact Us | Link to Us | Site Map
Copyright ? 2008. Academic Tutorials.com. All rights reserved Privacy Policies | About Us
Our Portals : Academic Tutorials | Best eBooksworld | Beyond Stats | City Details | Interview Questions | Discussions World | Excellent Mobiles | Free Bangalore | Give Me The Code | Gog Logo | Indian Free Ads | Jobs Assist | New Interview Questions | One Stop FAQs | One Stop GATE | One Stop GRE | One Stop IAS | One Stop MBA | One Stop SAP | One Stop Testing | Webhosting in India | Dedicated Server in India | Sirf Dosti | Source Codes World | Tasty Food | Tech Archive | Testing Interview Questions | Tests World | The Galz | Top Masala | Vyom | Vyom eBooks | Vyom International | Vyom Links | Vyoms | Vyom World | Important Websites
Copyright ? 2003-2024 Vyom Technosoft Pvt. Ltd., All Rights Reserved.