English 中文(简体)
将一台打字机印制成视窗,而不是隐含的CR+LF
原标题:Printing a typewriter LF to the Windows terminal, instead of the implied CR+LF

我知道,如何将新线特性(LF)仅仅写到幼年产出(见下面的测试法),但总是在终端会议所展示的案文的正确形象中转换成欧阵。

我在互联网上发现了几个类似的问题,其中包括关于中继流的2个问题:C++ - 标准产出的新线而不是Windows平台CR+LF的新线和,但它们和我发现有帮助的其他人都没有。

Here is a small example, to show you what I want:

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

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

The expected output

Hello
     Hi

The actual output

Hello
Hi

这是因为,线性饲料的特性被线性饲料和板块回收器所取代。 我尝试了好几件事情来消除这种行为,包括上述联系中提到的想法和更多的想法。

www.un.org/Depts/DGACM/index_spanish.htm 我预计,终端应用程序“

Test Code (which also doesn t work)

唯一具有某种视觉效果的是最后一次测试,但是它完全可以排除所有特殊性,因此,新路线只是新线,而是一种替代。 (Which不是应该发生的事情)

#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
");
    printf("hi");
    printf(&carriageReturn[0]);
    printf("sus");
    printf(&lineFeed[0]);
    printf("mogus");
    printf("hey world
sup
hi");

    // 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
");
    printf("hi");
    printf(&carriageReturn[0]);
    printf("sus");
    printf(&lineFeed[0]);
    printf("mogus");
    printf("hey world
sup
hi");

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

    printf("hello there
");
    printf("hi");
    printf(&carriageReturn[0]);
    printf("sus");
    printf(&lineFeed[0]);
    printf("mogus");
    printf("hey world
sup
hi");

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

    printf("hello there
");
    printf("hi");
    printf(&carriageReturn[0]);
    printf("sus");
    printf(&lineFeed[0]);
    printf("mogus");
    printf("hey world
sup
hi");

    // Setting output mode to binary. That way 
 will not be replaced with 

 by the C standard library implementation
    _setmode(1, _O_BINARY);
    _setmode(_fileno(stdout), O_BINARY);
    _setmode(_fileno(stdin), O_BINARY);

    printf("hello there
");
    printf("hi");
    printf(&carriageReturn[0]);
    printf("sus");
    printf(&lineFeed[0]);
    printf("mogus");
    printf("hey world
sup
hi");

    // 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
");
    printf("hi");
}

Feel free to provide links to helpful articles, sample code, more things I can test, links to documentation or other media. Or maybe an explaination what I did wrong.
Thank you for your time and answer.

最佳回答

进行屏幕印刷的方法是完全控制。 这符合我们在这一例子中所做的那样:put_char_con() 拦截每个果园,然后按我们的设计印制。

实例是超高技能,因为它包括如何做4类(而不是标准8)。 但是,我之所以离开,是因为我自认为你是一位方案家——也许你也更喜欢4个禁忌,而这一守则也规定控制(! !) 。

但请注意,我们确实提供了你要求的新线行为:

  • Current cursor X is unchanged
  • Current cursor Y is advanced to next line

If any case of the switch() statement isn t desired, you can comment out as you like. You ll find that the API WriteFile() does a very competent job of printing tab stops, backspaces... everything. So, you needn t intercept anything via cases except where you don t like its behavior... such as newline ( ).

该代码is可登陆其他平台。 只是为它使用的短片窗提供合适的替代品。 它们是:

  • GetConsoleScreenBufferInfo() -- used for finding current cursor position and screen size.
  • WriteFile() -- print one or more characters to the screen. Same behavior as printf().
  • SetConsoleCursorPosition() - like it sounds.

该守则今天由我汇编和测试:

#include <windows.h>
#include <stdio.h>

#define TAB_STOP 4

HANDLE hConOut;


/*-----------------------------------------------------------------------------
**  put_char_con()
**  
**  Writes a character to the console.
**  Each char value passed in parameter is examined -- then we print it how we
**  want it printed.
*------------------------------------------------------------------------------*/
int put_char_con(int ch)
{
    DWORD numBytesWritten = 0;
    int num_pad, cursor_offs;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    BOOL bSuccess;

    bSuccess = GetConsoleScreenBufferInfo(hConOut, &csbi);
    if(!bSuccess)
    {
        puts("GetConsoleScreenBufferInfoWr() failed in put_char_con()");
        return FALSE;
    }
    
    switch(ch)
    {
        case  
 :
            csbi.dwCursorPosition.Y++;
            /* For stdio functions, a newline also implies a carriage return.  If that s what
            * you want, enable this next line, else comment it out And cursor X coord stays put. */
            //csbi.dwCursorPosition.X = 0;
            numBytesWritten++;
        break;

        /* For stdio functions, TAB white-space-pads everything from 
        * the cursor to the tab stop.  You can also comment out this case and
        * let WriteFile() handle it -- which behaves just as printf() does*/
        case  	 :
            cursor_offs = csbi.dwCursorPosition.X;
            if(cursor_offs < 0)
                cursor_offs = 0;
            num_pad = TAB_STOP -((cursor_offs)%TAB_STOP);
            while(num_pad-- && (csbi.dwCursorPosition.X < csbi.srWindow.Right))
            {
                char ch =    ;
                WriteFile(hConOut, &ch, 1, &numBytesWritten, NULL);
                csbi.dwCursorPosition.X++;
            }
            GetConsoleScreenBufferInfo(hConOut, &csbi);
        break;

        default: /* Any other char */
            WriteFile(hConOut, &ch, 1, &numBytesWritten, NULL);
            GetConsoleScreenBufferInfo(hConOut, &csbi);
            numBytesWritten++;
        break;

    }
    SetConsoleCursorPosition( hConOut, csbi.dwCursorPosition);
    if(numBytesWritten)
        return ch;

    return EOF; //Usually defined as (-1); 
}


/*--------------------------------------------------------------------
**  cputs_con()
**  
**  Writes a string to the console.  
**  Uses:  put_char_con()
*--------------------------------------------------------------------*/
void cputs_con(const char *s)
{
    /* Code updated per comment from Ted Lyngmo -- thx Ted!*/
    while(*s)  
        put_char_con(*s++);     
}


int main() 
{
    if(!hConOut)
    {
        hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
        if(hConOut == INVALID_HANDLE_VALUE)
        {
            printf("STDOUT not available");
            return 0;
        }
    }

    puts("0   4   8   12         24      32      40      48      56      64      72      80");
    puts("|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |");
    
    cputs_con("This is a 	tab test. 	Each word 	following a tab should be 	tab aligned...

");
    cputs_con("This
is the newline test...If you want
carriage returns you must manually
add them to your strings.

");

    system("pause");
    return 0;
}

产出:


    0   4   8   12         24      32      40      48      56      64      72      80
    |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
    This is a   tab test.   Each word   following a tab should be   tab aligned...
    This
        is the newline test...If you want
    carriage returns you must manually
                                      add them to your strings.
问题回答

如果你的系统能够“虚拟终端”,你就能够使用VT100控制代码和ANSI进行这一和许多其他事情。 SYS。


#include <stdio.h>

int main( void ) {
    printf( "Hello World!" );
    printf( "33[1B" ); // Cursor down  n  rows
    printf( "Hi!" );

    return 0;
}

结果:

Hello World!
            Hi!

Here s a 90 second YT video showing how to enable this facility in Windows 10.
https://www.youtube.com/watch?v=HeJOyEw3RtM





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签