如何使用 CFSCRIPT 重写此代码块?
我正在使用 ColdFusion 8。
我试图在 CFSCRIPT 中编写此代码块,但不太明白。
<cfloop query="Q">
<cfscript>
// CREATE NEW LINE
NewLine = "";
NewLine = NewLine & Q.product_url;
</cfscript>
<cffile action="append" file="#ThisFile#" output="#NewLine#">
</cfloop>
这是 CFSCRIPT
// LOOP THROUGH QUERY RESULTS
for (i = 1; i lte Q.RecordCount; i=i+1) {
// CREATE NEW LINE
NewLine = "";
NewLine = NewLine & Q.product_url[i];
// READ THE FILE
File = fileOpen(ThisFile, "read");
// WRITE NEW LINE TO FILE
fileWriteLine(File, "#NewLine#");
fileWrite(ThisFile, File);
fileClose(File);
}
我知道我应该打开该文件,修改它,然后关闭该文件。我想我需要在每添加一个新行时都这样做。
这段代码有什么问题?
I am using ColdFusion 8.
I am trying to write this code block in CFSCRIPT, but can't quite get it.
<cfloop query="Q">
<cfscript>
// CREATE NEW LINE
NewLine = "";
NewLine = NewLine & Q.product_url;
</cfscript>
<cffile action="append" file="#ThisFile#" output="#NewLine#">
</cfloop>
Here is the CFSCRIPT
// LOOP THROUGH QUERY RESULTS
for (i = 1; i lte Q.RecordCount; i=i+1) {
// CREATE NEW LINE
NewLine = "";
NewLine = NewLine & Q.product_url[i];
// READ THE FILE
File = fileOpen(ThisFile, "read");
// WRITE NEW LINE TO FILE
fileWriteLine(File, "#NewLine#");
fileWrite(ThisFile, File);
fileClose(File);
}
I know I am supposed to open the file, modify it, then close the file. I think I need to do that with each new line added.
What's wrong with this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已修复:
已解决的问题:
您不必每行打开和关闭文件; a) 打开文件,b) 写入 1-x 行,然后 c) 关闭文件(或者,你可以使用
fileWrite()
一次性写入所有内容)你不需要fileWriteLine 和 fileWrite 都可以,fileWriteLine 用于逐行写入,而 fileWrite 用于一次性写入整组数据。
我还将“写入”更改为“追加”,以防第一次执行时您要写入的文件不存在——显然,如果您能保证目标文件存在,则可以将“追加”替换为“写”。请记住,更改此设置也会随着时间的推移而增加您的文件;它可能不是您想要的,因此如果是这种情况,请将其切换回“写入”。
虽然在没有看到您的确切错误的情况下我无法确定,但我有一种预感,
FileWrite()
行才是罪魁祸首。Fixed:
Issues addressed:
You don't open and close the file each line; you a) open file, b) write 1-x lines, then c) close the file (alternately, you can write it all at once with
fileWrite()
)You don't need both fileWriteLine and fileWrite, fileWriteLine is for line-by-line writing, while fileWrite is for writing and entire set of data in one shot.
I also changed the "write" to "append", in case the file you're writing to doesn't exist upon the first execution--obviously, if you can guarantee your destination file exists, you can replace "append" with "write". Keep in mind that changing this will also grow your file over time; it may not be what you want, so switch it back to "write" if this is the case.
Although I can't be certain without seeing your exact error, I have a hunch it was
FileWrite()
line that was the culprit.