<分区>
我想在 Tcl 中开发一个命令“redirect”,它将要执行的命令作为参数并将命令的输出重定向到用户提供的文件。我正在使用下面的代码并且运行良好。这段代码的唯一问题是文件是以 DOS 格式创建的。我该如何摆脱这种情况,因为格式应该是 Unix 风格而不是 DOS?
我尝试在 VI 编辑器和其他解决方法中设置 :set binary vim +"argdo setlocal ff=unix"+wqa 但是,找不到以 DOS 格式创建此文件的任何原因。
Tcl_Channel stdoutChannel = Tcl_GetStdChannel(TCL_STDOUT);
Tcl_Channel stderrChannel = Tcl_GetStdChannel(TCL_STDERR);
if(stdoutChannel) {
Tcl_Flush(stdoutChannel);
}
if(stderrChannel) {
Tcl_Flush(stderrChannel);
}
FILE* filePtr = fopen(filename, "wb");
int pfd = fileno(filePtr);
int saved = dup(1);
close(1);
Tcl_Obj* commandResult = Tcl_NewStringObj(command,strlen(command));
dup2(pfd,STDOUT_FILENO);
Tcl_EvalObj(TBXslaveInterp,commandResult);
close(pfd);
fflush(stdout);
// restore it back
dup2(saved, 1);
close(saved);
return TCL_OK;
输出
Command executed :
redirect myfile {puts "Inside Tcl"}
Viewing File in VI at the bottom shows:
"myfile" [dos] 1L, 12C
cat myfile
Inside Tcl^M
注意 ^M 在执行 cat 操作时打印。这是因为保存文件的 DOS 样式格式。