【问题标题】:TCL C API Create and Register new ChannelTCL C API 创建和注册新频道
【发布时间】:2023-09-09 15:59:01
【问题描述】:

我使用 Tcl 8.6(windows),无法注册和使用新频道

std::ofstream file("1.txt");

Tcl_Channel kanal = Tcl_CreateChannel(Type, "myChann", file, TCL_WRITABLE);
Tcl_RegisterChannel(interp, kanal);

file.close();

类型是

Tcl_ChannelType* Type = new Tcl_ChannelType();
Type->closeProc = closeProc;
Type->inputProc = inputProc;
Type->outputProc = outputProc;
Type->typeName = "My own chann";
Type->version = TCL_CHANNEL_VERSION_2;

函数很简单,它们只有 std::cout

我用脚本运行解释器

"chan puts myChan whatever"

什么也没发生,解释器没有错误,没有输出(控制台,文件)。 我不知道怎么咬这个, 这是第一个目标,创建新的香奈儿并使用它, 第二个是用我自己的频道替换TCL_STDOUT(可以是std::ofstream), 这样当我用

运行解释器时
"puts WhatEver"

该字符串转到 std::ofstream

【问题讨论】:

    标签: c++ c tcl ofstream channels


    【解决方案1】:

    可能你没有刷新输出。

    这可以通过

    flush myChan
    

    您还可以将通道配置为在行尾或任何写入内容时刷新:

    chan configure myChan -buffering line
    

    (或none)。 stdout 默认配置为-buffering line

    【讨论】:

    • 当我“chan flush myChann”时,当我返回 0 时,我得到了从 Tcl_ChannelType* 运行 outputProc 的无限循环。当我返回 int toWrite 时没有发生任何事情
    • 进度,我的功能被收集,但文件仍然是空的。我怎样才能得到这个 std::ostream ?
    • @user 您确实需要正确实现 outputProc,返回实际写入的字节数。如果您始终返回 0,则刷新代码将再次尝试。 :-)
    • 我怎样才能得到正确的 std::ostream ?因为当我创建频道时,我在 ClientData std::ofstream 中选择,我可以在 outputProc 中获得那个 std::ostream 吗?可能来自 ClientData,但我不能...
    • 将其转换为 std::ostream。
    最近更新 更多