【问题标题】:How to remove an empty lines while exporting the text files?导出文本文件时如何删除空行?
【发布时间】:2019-06-01 10:14:38
【问题描述】:
DEFINE VARIABLE cExportData AS CHARACTER NO-UNDO FORMAT 'X(250)'.
DEFINE VARIABLE cPath AS CHARACTER NO-UNDO.
DEFINE VARIABLE cExt  AS CHARACTER NO-UNDO.
DEFINE VARIABLE cSFTL AS CHARACTER NO-UNDO FORMAT 'X(150)'.
DEFINE VARIABLE cMessageDateTime AS CHARACTER NO-UNDO.

ASSIGN
      cPath  = "R:\Downloads\progress\".
      cExt   = ".Txt".
      cMessageDateTime = "123456789".



OUTPUT TO VALUE (cPath + cMessageDateTime + STRING(MTIME) + cExt ).   


     cExportData = "Data1" + CHR(10) + "Data2" + CHR(10) + "Data3" + CHR(10) + "END.".
     MESSAGE  cExportData.

OUTPUT TO CLOSE.

所以当我看到使用记事本++ 导出的文本文件时,我可以看到 Data1、Data2、Data3 的前 3 行,但第 4 行是用空创建的。如何停止创建空行。

【问题讨论】:

    标签: openedge progress-4gl


    【解决方案1】:

    MESSAGE 通常不是您想要用于输出到文件的内容,它具有许多额外的行为,特定于在提供错误消息等的上下文中与用户交互。PUT 通常更适合写入文件。嵌入 CHR(10) 也不是一个好主意——这是一个非常特定于操作系统的行终止符。 CHR(10) 是 Unix 风格的换行符,但您显然是在 Windows 上运行的(它使用 CHR(10) + CHR(13)。

    我可能会重写你的代码如下:

    DEFINE VARIABLE cExportData AS CHARACTER NO-UNDO FORMAT 'X(250)'.
    DEFINE VARIABLE cPath AS CHARACTER NO-UNDO.
    DEFINE VARIABLE cExt  AS CHARACTER NO-UNDO.
    DEFINE VARIABLE cSFTL AS CHARACTER NO-UNDO FORMAT 'X(150)'.
    DEFINE VARIABLE cMessageDateTime AS CHARACTER NO-UNDO.
    
    /* the "." that you had at the ends of the ASSIGN sub statements
     * is turning it into 3 distinct statements, not one as your 
     * indentation shows
     */
    
    ASSIGN
      cPath  = "R:\Downloads\progress\" 
      cExt   = ".Txt"
      cMessageDateTime = "123456789"
    . /* end the ASSIGN statement */
    
    /* if you are using MTIME because you imagine it will make your
     * filename unique then you are mistaken, on a multi-user or 
     * networked system it is trivial for 2 processes to create files
     * at the very same MTIME
     */
    
    OUTPUT TO VALUE (cPath + cMessageDateTime + STRING(MTIME) + cExt ).   
    
    /* usually some kind of looping structure would output each line
     * building the whole output by concatenating into a string will
     * eventually exhaust memory.
     */ 
    
    put unformatted "Data1" skip "Data2" skip "Data3" skip "End." skip.
    
    /* the final SKIP might not be needed - it is unclear to me
     * if that is a problem for your client
     */
    
    /* as originally written this creates
     * an empty file called "CLOSE"
     */
    
    OUTPUT /*** TO ***/ CLOSE.
    

    【讨论】:

    • 那么如果我们想获得唯一的文件名,那么最好的方法是什么?
    • 因此,如果我通过连接到字符串来构建整个输出,最终会耗尽内存。如果是这样,最好的方法是什么?你能告诉先生吗?
    • 如果您的数据确实只是几个名称简单的字段,那么您显示的内容就可以了,但更常见的是,您的数据将来自一些循环的代码,而不是来自少数几个离散命名的变量。所以 OUTPUT TO 通常在循环之外,PUT 语句将在循环内并使用与查询相关的字段,最后,OUTPUT CLOSE 通常在循环结束之后。
    • 您可能想问一个关于唯一文件名的新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 2013-08-25
    • 2011-12-29
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多