将打字机 LF 打印到 Windows 终端,而不是隐含的 CR+LF

问题描述 投票:0回答:1

我知道如何将换行符(LF)写入控制台输出(您可以在下面的测试代码中看到它),但它总是在终端中显示的文本的渲染图像中转换为 CRLF会议。

我在互联网上发现了一些类似的问题,包括 stackoverflow 上的两个问题:C++ - 在 Windows 平台上将换行符打印为 LF 而不是 CR+LF 到标准输出有没有办法将 stdout 设置为二进制模式?但是我发现他们和其他人都没有帮助。

这是一个小例子,向您展示我想要的:

#include <stdio.h>
// possibly other includes, defines and more

void main(void) {
    printf("Hello\n"); // Where \n is a normal Line Feed Character (0x0A)
    printf("Hi");
}

预期产出

Hello
     Hi

实际产量

Hello
Hi

这是因为,换行符被替换为换行符和回车符。我尝试了几种方法来禁用此行为,包括上面链接中提到的想法等等。

我希望必须在终端应用程序中更改此设置,因为这就是它在某些 Linux 命令行中的工作方式。但是Windows控制台应用程序的设置中没有这样的设置。也许有一个注册表项,但我在互联网上找不到任何提及它的信息,我自己也找不到潜在的密钥。

测试代码(也不起作用)

唯一有一些视觉效果的是最后一个测试,但它完全禁用了特殊字符的所有渲染,因此换行符不再是换行符,而是白框作为替代品。 (这不是应该发生的事情)

#include <iostream>
#include <Windows.h>
#include <CommCtrl.h>

#include <io.h>
#include <fcntl.h>

int main() {
    char lineFeed[] = { 0x0A, 0x00 };
    char carriageReturn[] = { 0x0D, 0x00 };

    // Testing without any settings changed
    printf("hello there\n");
    printf("hi");
    printf(&carriageReturn[0]);
    printf("sus");
    printf(&lineFeed[0]);
    printf("mogus");
    printf("hey world\rsup\nhi");

    // Toggling what [ENTER] does in regards to edit controls
    HWND consoelWindow = GetConsoleWindow();
    SetWindowLongA(consoelWindow, ES_WANTRETURN, GetWindowLongA(consoelWindow, ES_WANTRETURN) ^ ES_WANTRETURN);

    printf("hello there\n");
    printf("hi");
    printf(&carriageReturn[0]);
    printf("sus");
    printf(&lineFeed[0]);
    printf("mogus");
    printf("hey world\rsup\nhi");

    // Disabling insertion of soft line-break characters
    SendMessage(consoelWindow, EM_FMTLINES, FALSE, 0);

    printf("hello there\n");
    printf("hi");
    printf(&carriageReturn[0]);
    printf("sus");
    printf(&lineFeed[0]);
    printf("mogus");
    printf("hey world\rsup\nhi");

    // Enabling insertion of soft line-break characters
    SendMessage(consoelWindow, EM_FMTLINES, TRUE, 0);

    printf("hello there\n");
    printf("hi");
    printf(&carriageReturn[0]);
    printf("sus");
    printf(&lineFeed[0]);
    printf("mogus");
    printf("hey world\rsup\nhi");

    // Setting output mode to binary. That way \n will not be replaced with \n\r by the C standard library implementation
    _setmode(1, _O_BINARY);
    _setmode(_fileno(stdout), O_BINARY);
    _setmode(_fileno(stdin), O_BINARY);

    printf("hello there\n");
    printf("hi");
    printf(&carriageReturn[0]);
    printf("sus");
    printf(&lineFeed[0]);
    printf("mogus");
    printf("hey world\rsup\nhi");

    // Disabling processed output, aka "Backspace, tab, bell, carriage return, and line feed characters are [not] processed"
    HANDLE outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);

    DWORD mode = 0;

    BOOL ret1 = GetConsoleMode(outputHandle, &mode);

    mode ^= ENABLE_PROCESSED_OUTPUT;

    BOOL ret2 = SetConsoleMode(outputHandle, mode);

    ret2 = ret2;

    printf("Hello There2\n");
    printf("hi");
}

随时提供有用文章、示例代码、我可以测试的更多内容的链接、文档或其他媒体的链接。或者也许解释一下我做错了什么。
谢谢您的时间和回答。

c windows cmd escaping
1个回答
0
投票

\n
将被您的终端解释为返回到新行的开头,因此除非您可以关闭它,否则您不能使用
\n
来执行此操作。

但是,您可以使用垂直选项卡

\v
获得类似的结果,具体取决于终端如何解释它。许多人会按照您的意愿跳到下一行:

void main(void) {
    printf("Hello\v");
    printf("Hi");
}
© www.soinside.com 2019 - 2024. All rights reserved.