Sml 将文件输出到stdin

Sml 将文件输出到stdin,sml,smlnj,Sml,Smlnj,如何准确地将文件中的字符输出到SML/NJ中的stdin?这是我到目前为止所做的,但我现在陷入了困境,因为我得到了编译器返回给我的错误 代码: 有没有想过我会错在哪里 好吧,这取决于您试图对文件输入做什么。如果只想打印从文件中读取的字符,而不将其输出到另一个文件,则可以只打印输出: fun outputFile infile = let val ins = TextIO.openIn infile; fun helper copt = (case copt of NONE =>

如何准确地将文件中的字符输出到SML/NJ中的stdin?这是我到目前为止所做的,但我现在陷入了困境,因为我得到了编译器返回给我的错误

代码:


有没有想过我会错在哪里

好吧,这取决于您试图对文件输入做什么。如果只想打印从文件中读取的字符,而不将其输出到另一个文件,则可以只打印输出:

fun outputFile infile = let
  val ins = TextIO.openIn infile;

  fun helper copt = (case copt of NONE => TextIO.closeIn ins 
                     | SOME c => print (str c); helper (TextIO.input1 ins));
in
  helper (TextIO.input1 ins)
end;


outputFile "outtest";    (*If the name of your file is "outtest" then call this way*)
然而,上面的例子是不好的,因为它将给您无限循环,因为即使它没有命中,也不知道如何终止和关闭文件。因此,此版本更清晰、可读性更强,并且:

fun outputFile infile = let
  val ins = TextIO.openIn infile;

  fun helper NONE = TextIO.closeIn ins 
    | helper (SOME c) = (print (str c); helper (TextIO.input1 ins));

in
  helper (TextIO.input1 ins)
end;


outputFile "outtest";
如果您只想将
infle
的内容输出到另一个文件,那么这就是另一个故事,在这种情况下,您必须打开一个文件句柄进行输出

fun outputFile infile = let
  val ins = TextIO.openIn infile;

  fun helper NONE = TextIO.closeIn ins 
    | helper (SOME c) = (print (str c); helper (TextIO.input1 ins));

in
  helper (TextIO.input1 ins)
end;


outputFile "outtest";