如何使用 CFSCRIPT 重写此代码块?

发布于 2024-12-29 08:31:41 字数 795 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2025-01-05 08:31:41

已修复:

<cfscript>
    myFile = fileOpen(ThisFile, "append");

    for (i = 1; i lte Q.RecordCount; i=i+1) {
        NewLine = "";
        NewLine = NewLine & Q.product_url[i];

        fileWriteLine(myFile, "#NewLine#");
    }

    fileClose(myFile);
</cfscript>

已解决的问题:

  1. 您不必每行打开和关闭文件; a) 打开文件,b) 写入 1-x 行,然后 c) 关闭文件(或者,你可以使用 fileWrite() 一次性写入所有内容)

  2. 你不需要fileWriteLine 和 fileWrite 都可以,fileWriteLine 用于逐行写入,而 fileWrite 用于一次性写入整组数据。

我还将“写入”更改为“追加”,以防第一次执行时您要写入的文件不存在——显然,如果您能保证目标文件存在,则可以将“追加”替换为“写”。请记住,更改此设置也会随着时间的推移而增加您的文件;它可能不是您想要的,因此如果是这种情况,请将其切换回“写入”。

虽然在没有看到您的确切错误的情况下我无法确定,但我有一种预感, FileWrite() 行才是罪魁祸首。

Fixed:

<cfscript>
    myFile = fileOpen(ThisFile, "append");

    for (i = 1; i lte Q.RecordCount; i=i+1) {
        NewLine = "";
        NewLine = NewLine & Q.product_url[i];

        fileWriteLine(myFile, "#NewLine#");
    }

    fileClose(myFile);
</cfscript>

Issues addressed:

  1. 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() )

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文