diff --git a/vlib/os/file.c.v b/vlib/os/file.c.v index 1c219b45ef..4d634c69a2 100644 --- a/vlib/os/file.c.v +++ b/vlib/os/file.c.v @@ -109,8 +109,7 @@ pub fn open_file(path string, mode string, options ...int) !File { if fd == -1 { return error(posix_get_error_msg(C.errno)) } - fdopen_mode := mode.replace('b', '') - cfile := C.fdopen(fd, &char(fdopen_mode.str)) + cfile := C.fdopen(fd, &char(mode.str)) if isnil(cfile) { return error('Failed to open or create file "${path}"') } diff --git a/vlib/os/file_test.v b/vlib/os/file_test.v index 31c838d8ef..a4fe309d07 100644 --- a/vlib/os/file_test.v +++ b/vlib/os/file_test.v @@ -450,3 +450,30 @@ fn test_open_file_on_chinese_windows() { assert os.file_size('中文.txt') == 2 } } + +fn test_open_file_crlf_binary_mode() { + teststr := 'hello\r\n' + fname := 'text.txt' + + mut wfile := os.open_file(fname, 'w', 0o666)! + wfile.write_string(teststr)! + wfile.close() + + mut fcont_w := os.read_file(fname)! + + os.rm(fname) or {} + + mut wbfile := os.open_file(fname, 'wb', 0o666)! + wbfile.write_string(teststr)! + wbfile.close() + + mut fcont_wb := os.read_file(fname)! + + os.rm(fname) or {} + + $if windows { + assert fcont_w != teststr + } + + assert fcont_wb == teststr +}