Io SML:为什么没有新的生产线可以';不得将其写入文件中,并使用“文件”\";

Io SML:为什么没有新的生产线可以';不得将其写入文件中,并使用“文件”\";,io,sml,Io,Sml,下面是一个将int列表写入文件的简单程序: fun write(num_list, file) = let val output = TextIO.openOut file fun num(nil) = TextIO.closeOut output | num(n::ns) = (TextIO.output(output, Int.toString(n)); TextIO.output(output, "\n"); num(ns)) in num(num_

下面是一个将int列表写入文件的简单程序:

fun write(num_list, file) = 
let 
    val output = TextIO.openOut file
    fun num(nil) = TextIO.closeOut output
      | num(n::ns) = (TextIO.output(output, Int.toString(n)); TextIO.output(output, "\n"); num(ns))
in
    num(num_list)
end;

为什么在打印每个数字后没有新行写入文件?

似乎代码正常工作,并且在每个数字后都会写入新行字符

我已经为您的
write
函数提供了一个替代定义,但这两个函数似乎都能工作

fun writeInts (ints, filename) = 
    let val fd = TextIO.openOut filename
        val _ = List.app (fn i => TextIO.output (fd, Int.toString i ^ "\n")) ints
        val _ = TextIO.closeOut fd
    in () end

fun read filename =
    let val fd = TextIO.openIn filename
        val content = TextIO.inputAll fd
        val _ = TextIO.closeIn fd
    in content end

val test = (writeInts ([1,2,3,4], "hello.txt"); read "hello.txt" = "1\n2\n3\n4\n")

看起来您的代码正常工作,并且在每个数字后都会写入一个换行符

我已经为您的
write
函数提供了一个替代定义,但这两个函数似乎都能工作

fun writeInts (ints, filename) = 
    let val fd = TextIO.openOut filename
        val _ = List.app (fn i => TextIO.output (fd, Int.toString i ^ "\n")) ints
        val _ = TextIO.closeOut fd
    in () end

fun read filename =
    let val fd = TextIO.openIn filename
        val content = TextIO.inputAll fd
        val _ = TextIO.closeIn fd
    in content end

val test = (writeInts ([1,2,3,4], "hello.txt"); read "hello.txt" = "1\n2\n3\n4\n")

好吧…那可能是因为我的系统??您知道为什么这在某些系统上不起作用吗?是的,可能您的系统使用了不同的线路终止顺序。在Windows上,这将是
“\r\n”
@SimonShine,文本流SML按照预期进行换行转换,因此无需在Windows上写入“\r\n”(事实上,这样做是错误的)。@Silvia.H,您在哪个系统上,您是如何验证没有写入换行的?Windows 10。我运行程序,打印的数字之间没有空格或新行。好吧……那可能是因为我的系统??您知道为什么这在某些系统上不起作用吗?是的,可能您的系统使用了不同的线路终止顺序。在Windows上,这将是
“\r\n”
@SimonShine,文本流SML按照预期进行换行转换,因此无需在Windows上写入“\r\n”(事实上,这样做是错误的)。@Silvia.H,您在哪个系统上,您是如何验证没有写入换行的?Windows 10。我运行程序,打印的数字之间没有任何空格或新行。