C language practice (III)

Source: Internet
Author: User

1. The clear screen function clrscr is unique to TC. If other C Language Environments do not have this function, no header file will contain this function.

We recommend that you use system ("CLS"); to replace clrscr (); is more common and has better compatibility.

The system () function is in # include <stdlib. h>.

/*

Printf ("| the program will show: | ");

Printf ("/R"); Press ENTER

*/

2. There is no gotoxy (x, y) in the conio. h header file in VC)

Define it by yourself.

# Include "windows. H"

Void gotoxy (int x, int y)

{

Coord C;

C. x = X-1;

C. Y = Y-1;

Setconsolecursorposition (getstdhandle (std_output_handle), C );

}

3. Obtain the cursor position function:

Bool getconsolescreenbufferinfo (handle hconsoleoutput, pconsole_screen_buffer_info lpconsolescreenbufferinfo );

Lpconsolescreenbufferinfo-> dwcursorposition. x returns the X coordinate of the cursor position.

Lpconsolescreenbufferinfo-> dwcursorposition. y returns the Y coordinate of the cursor position.

Set the cursor function:

Bool setconsolecursorposition (handle hconsoleoutput, coord dwcursorposition );

Dwcursorposition. x cursor position X coordinate

Dwcursorposition. y cursor position y coordinate

Note: contains files for Windows. h.

Example:

# Include <windows. h>

# Include <stdio. h>

Int main ()

{

Lele_screen_buffer_info csbinfo;

Coord cursorpos;

Cursorpos. x = 10;

Cursorpos. Y = 10;

Getconsolescreenbufferinfo (getstdhandle (std_output_handle), & csbinfo );

Setconsolecursorposition (getstdhandle (std_output_handle), cursorpos );

Printf ("123456/N ");

Setconsolecursorposition (getstdhandle (std_output_handle), csbinfo. dwcursorposition );

Return 0;

}

4,

# Include <dos. h>

# Include <system. h> sleep () seconds -------- Linux # include "windows. H "# include" WINBASE. H "sleep () millisecond ------------ MFC vc6 no delay () sleep ()

 A> the prototype of the sleep function in MFC is:

void Sleep(  DWORD dwMilliseconds);
B> the sleep function in Linux is prototype:
unsigned int sleep(unsigned int seconds);
In MFC, it is in microseconds, and in Linux, it is in seconds. In Linux, the thread sleep function in microseconds is:
void usleep(unsigned long usec);          int usleep(unsigned long usec); /* SUSv2 */
Or you can use the select function + timeval structure (up to microseconds ),
Or use the pselect Function + timespec (it can be accurate to nanoseconds, accurate enough !)

5. In Linux, all this is very simple, and no function is required. printf can be completed.

Clear screen: printf ("% C [2j", 27 );

Jump cursor to screen x (1 ~ 25) rows, y (1 ~ 80) column: printf ("% C [% d; % DH", 27, x, y );

Functions such as getch are too junk. in Linux, you can control whether all input functions will be displayed, including scanf and gets. The method is to disable echo during input: system ("stty-echo"); Enable echo during input: System ("stty echo ");

6. In Windows and Linux, the X and Y coordinates are mutually adjusted, Which is inconsistent;

Printf ("% C [% d; % DH", 27, p, y );

Printf ("*****"); // No/N, but it cannot be printed.

7. getch () is a non-portable function and can be simulated by yourself. The test code is as follows:

# Include <stdio. h>

# Include <termios. h>

# Include <unistd. h>

# Include <assert. h>

# Include <string. h>

Int getch (void );

Int main (void)

{

Char ch;

Printf ("input a char :");

Fflush (stdin );

Ch = getch ();

Printf ("/Nyou input a % C/N", CH );

Return 0;

}

Int getch (void)

{

Int C = 0;

Struct termios org_opts, new_opts;

Int res = 0; // ----- store old settings -----------

Res = tcgetattr (stdin_fileno, & org_opts );

Assert (RES = 0); // ---- set new terminal parms --------

Memcpy (& new_opts, & org_opts, sizeof (new_opts ));

New_opts.c_lflag & = ~ (Icanon | echo | echoe | echok | echonl | echoprt | echoke | icrnl );

Tcsetattr (stdin_fileno, tcsanow, & new_opts );

C = getchar (); // ------ restore old settings ---------

Res = tcsetattr (stdin_fileno, tcsanow, & org_opts );

Assert (RES = 0 );

Return C;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.