File io 从文本文件中读取字符

File io 从文本文件中读取字符,file-io,postscript,File Io,Postscript,我有一个任务,从一个文本文件中逐个读取字符,然后使用PostScript将它们写入另一个文本文件。我已经到了可以从一个文本文件中逐行读取并将该行写入另一个文本文件的地步 这个版本有效 %!!PS 但是当我尝试读取单个字符时,我会得到一个错误,说它的类型不匹配,我不确定如何解决它 代码如下: /infile (input.txt) (r) file def % open files and save file objects /outfile (output.txt) (w) file def /

我有一个任务,从一个文本文件中逐个读取字符,然后使用PostScript将它们写入另一个文本文件。我已经到了可以从一个文本文件中逐行读取并将该行写入另一个文本文件的地步

这个版本有效

%!!PS

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

代码如下:

/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

所以。。。错误发生在哪里?读,写?还有别的吗

无论何时调试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时,您正试图写入outfile您的%else子句,这确实不起作用,因为'read'没有在堆栈上留下任何内容,它没有读取任何内容,这就是它返回false的原因。我怀疑这是你的问题,尽管除了桌面检查,我还没有试着调试程序

    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