By: Harsha .k
How to read and write Traditional Files.

Why you need the files, why can’t I use database, well for Simplicity, Efficiency, and Customization only choice is Traditional Files.

You need them when you want to store the click values that the number of user visited your sites and read them.

This Tutorial will show you how to read and write in to files
<cfile> tag can be use fro basic action like read, write , upload ,move, delete ,etc..

Reading ?

If you need to read the file eg: "c:\sample.txt"

You can read a file by issuing the bellow tag.

<cffile action="read" file="C:\sample.txt" variable="ihold">

where ihold will hold the file contents of file.
After read tag where ever You print ,using #ihold# it will print contents of file.

Below is an example, for this u need to create a file in "c:\sample.txt" and write what ever you want to display at run time.

example.cfm

<html>
  <head>
     <title>
Untitled Document</title>
     <meta http-equiv="Content-Type" content=
"text/html; charset=iso-8859-1">
  </head>

<body>

<cffile action="read" file="c:\sample.txt" variable="iload" >

<!--- read from c:\sample.txt and holds the content in iload
iload<------------c:\samplea.txt --->

<cfoutput>
  <p> This Should See The Contents Of Files </p>
  #iload#
  <!--- print the contents of text files --->
</cfoutput>

</body>
</html>

Writing ?

If you need to write to the eg: file "c:\sample.txt" the message "hello write me"

First declare a varible for storing "hello write me" or u can directly pass it in single quotes in output option of cffile tag(output=’hello write me’).

<cfset putme="hello write me">

You can write a file by issuing the bellow tag.

<cffile action="write" file="c:\sample.txt" output="putme" >

alert! this will erase all contents previously contained data in sample.txt.

If u don’t want to do so, then use append instead of write. this will append to the previous data.(or read the contents to of the file to ihold .use output =’#ihold# #putme#’ old contents plus new)

<cffile action="append" file="c:\sample.txt" output="putme">

Below is an example ,for this u need to create a file in "c:\sample.txt" and write what ever you want to display at run time.

example.cfm

<html>
  <head>
     <title>
Untitled Document</title>
     <meta http-equiv="Content-Type" content=
"text/html; charset=iso-8859-1">
  </head>

<body


<------------put ----------->
<cfset putme= "hello write me">

<cffile action="write" file="c:\sample.txt" output="putme" >

<!--- write contents of varible putme to c:\sample.txt ,
c:\samplea.txt = c:\samplea.txt+putme------->

<-------------This is for printing the changes----------------------->
<cfoutput>

<cffile action="read" file="c:\sample.txt" variable="iload" >

<p> This Should See The New Contents Of Files </p>
#iload#
<!--- print the contents of text files --->

</cfoutput>

</body>
</html>

Now u can store the hit counter of your website in a file, And read it,
Increment (process) it and write back to file.

About This Tutorial
Author: Harsha .k
Skill Level: Intermediate 
 
 
 
Platforms Tested: CF2,CF3,CF4,CF5,CFMX,BlueDragon
Total Views: 102,112
Submission Date: December 28, 2004
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • I have a selection of text files in a directory and I need to read the contents of a selected text file from a drop-down selection box into a variable. Any idea how I could accomplish this?

Advertisement

Sponsored By...
Mobile App Development (IOS, Android, Cordova, Phonegap, Objective-C, Java) - Austin, Texas Mobile Apps - Touch512, LLC.