-
1. Re: Writing to a file
BKBK Jul 23, 2014 6:43 AM (in response to Cold_Fish)<cfset fileName = expandPath("counter.txt")>
<cflock name="fileReadLock" type="exclusive" timeout="20" throwontimeout="yes">
<cfif fileExists(fileName)>
<cffile action="read" file="#fileName#" variable="fileContent">
<cfif not isNumeric(fileContent)>
<cfthrow message="Content of file counter.txt is non-numerical.">
<cfabort>
</cfif>
<cfset fileContent = fileContent+1>
<cffile action="write" file="#expandpath('counter.txt')#" output="#fileContent#">
<cfelse>
<cfthrow message="Counting file counter.txt is in the wrong location or does not exist.">
<cfabort>
</cfif>
Current count: <cfoutput>#fileContent#</cfoutput>
</cflock>
-
2. Re: Writing to a file
Cold_Fish Jul 24, 2014 7:07 AM (in response to BKBK)BKBK!!!
That was exactly what I needed! I implemented it into my current code and it worked perfectly! Thank-you so much for your help with this! It is very much appreciated!
Graham. -
3. Re: Writing to a file
fergusondj Jul 24, 2014 7:24 AM (in response to Cold_Fish)While BKBK does give you a way that it will work I am wondering why you would even do this? This has more ways it could fail than I can count. Among them, you could easily have a lock left behind on the file thus making it unreadable. You should really be storing this data in a database not a file.
--Dave
-
4. Re: Writing to a file
Scott Stroz Jul 24, 2014 7:28 AM (in response to Cold_Fish)This is an incredibly bad way to manage this information - so bad it cannot be measured with existing technology.
Store the number in a DB, not in a file to be read/written to frequently. Too many factors that can cause this to go belly up.
-
5. Re: Writing to a file
Cold_Fish Jul 24, 2014 7:51 AM (in response to Cold_Fish)Thank-you for your concerns.
I am open to other methods to accomplish the same thing, but need a little more help than simply "write it to a database". I would likely need use MSAccess as the database for this counter if I'm going to go this method.
I'm willing to learn!
Graham.
-
6. Re: Writing to a file
fergusondj Jul 24, 2014 8:03 AM (in response to Cold_Fish)Your application doesn't use a database at all? I find that a little strange and at the same time Interesting. First, don't use MS Access for well, anything. If you don't have a DB use something like Mysql. It is free and works very well.
-
7. Re: Writing to a file
BKBK Jul 24, 2014 9:26 AM (in response to Cold_Fish)Circumstances may justify file storage (though, as you later revealed, that doesn't apply to you). Think, for example, of the absence of a database connection or of a database with a lock and excessive traffic. File storage even has pride of place in certain areas. Most applications in fact store configuration information on the file system, which they then update throughout the working life of the application.
Having said that, I should hasten to add that I agree with the other posters: a database is preferable to a file for this kind of storage. However, I would be less negative in my appraisal of file storage in general. At least, not until I know the circumstances.
I would choose MySQL. If for whatever reason you have to use MS Access - I have heard about a System Admin who imposed it on his Developer colleagues - then do so by all means. It will still be a step in the right direction.



