从文本文件中读取字符

时间:2014-02-09 03:56:24

标签: file-io postscript

我有一个从一个文本文件中逐个读取字符的任务,并使用post script将它们写入另一个文本文件。我可以从文本文件中逐行读取并将该行写入另一个文件。

此版本有效......

%!PS

/infile (input.txt) (r) file def % open files and save file objects
/outfile (output.txt) (w) file def
/buff 128 string def % your buffer for reading operations
{ % loop
        infile buff readstring
        { %ifelse
                outfile exch writestring
        }
        { %else
                outfile exch writestring
                infile closefile
                outfile closefile
                exit % exit the loop
        } ifelse
} bind loop

但是当我尝试阅读单个字符时,我得到一个错误,说明它的类型匹配,我不确定如何解决它。

以下是代码:

/infile (input.txt) (r) file def % open files and save file objects
/outfile (output.txt) (w) file def
/buff 1 string def % your buffer for reading operations
{ % loop
        infile buff read
        { %ifelse
                outfile exch write
        }
        { %else
                outfile exch write
                infile closefile
                outfile closefile
                exit % exit the loop
        } ifelse
} bind loop

1 个答案:

答案 0 :(得分:0)

那么......错误发生在哪里?读,写?别的什么?

每当你调试PostScript时,这应该是一个重要的起点,找出哪个运算符抛出你的错误,以及你的程序中抛出错误的位置。如果你不确定撒上'print'和'=='来跟随执行线程。例如

    infile buff read
    { %ifelse
            (in read loop, character read is ) print dup == flush
            outfile exch write
    }
    { %else
            outfile exch write
            infile closefile
            outfile closefile
            exit % exit the loop
    } ifelse

请注意,当'read'返回false(你的%else子句)时,你试图写入outfile,这实际上是行不通的,因为'read'没有在堆栈上留下任何东西(它没有'读任何东西,这就是它返回错误的原因)。我怀疑这是你的问题,虽然除了桌面检查之外我还没有尝试调试程序。