C + + Console programming (not yet tidy up the garbage)

Source: Internet
Author: User
Tags clear screen tidy

--------------------------------file-----------------------------------
Fin.open (file name); Open file read-in, not empty file
Fin.close (); Close file read-in, must, otherwise cannot open another file
Fout.open (file name); Open file output, will empty the file
Fout.close (); Close the file output, be sure, otherwise you cannot open another file
Ofstream (file name); Create a file
DeleteFile (file name); Delete file
If you want to open a file, the file name is given a string or string plus a string, so write it like this:
String s= "123456";
Fin.open ((s+ ". txt"). C_STR ());
Fout.open ((s+ ". txt"). C_STR ());
To create and delete files:
String s= "123456";
Ofstream ((s+ ". txt"). C_STR ());
DeleteFile ((s+ "txt"). C_STR ());
You can also write this:
String s= "123456.txt";
Fin.open (S.c_str ());
Fout.open (S.c_str ());
Ofstream (S.c_str ());
DeleteFile (S.c_str ());

fin>>x; Read X in File
fout<<x; Export x to the file

-------------------------------font Color--------------------------------
HANDLE hout=getstdhandle (std_output_ HANDLE);//Handle
Setconsoletextattribute (hout,foreground_intensity| foreground_red| foreground_green| Foreground_blue);//Set Tri-color addition, white
Setconsoletextattribute (hout,foreground_intensity| foreground_red);//Set red, Red
Setconsoletextattribute (hout,foreground_intensity| Foreground_green);//Set green, Green
Setconsoletextattribute (hout,foreground_intensity| Foreground_blue);//Set Blue, Blue
Setconsoletextattribute (hout,foreground_intensity| foreground_red| Foreground_green);//Set red and green Add, Yellow
Setconsoletextattribute (hout,foreground_intensity| foreground_red| Foreground_blue);//Set red and blue Add, Pink
Setconsoletextattribute (hout,foreground_intensity| foreground_green| Foreground_blue);//Set green and blue Add, Cyan
Setconsoletextattribute (hout,foreground_intensity);//Set the color, no color added, it is the primary colors

Setconsoletextattribute (Hout,a);
Range of values for a
Font Color:
1 dark Blue
2 dark Green
3 Dark Cyan
4 dark red
5 Dark Pink
6 Yellow
7 Dark White
8 Grey
9 Light Blue
10 Light Green
11 Light Cyan
12 Light Red
13 Light Pink
14 Light Yellow
15 Light White

Background color:
1~15 Black
16~31 dark Blue
32~47 Dark Green
48~63 Dark Cyan
64~79 Dark Red
80~95 Dark Pink
96~111 Dark Yellow
112~127 Dark White
128~143 Grey
144~159 Light Blue
160~175 Light Green
176~191 Light Cyan
192~207 Light Red
208~223 Light Pink
224~239 Light Yellow
240~255 Light White

How to use: For example, dark red background color, light white font color, A is 64+15=79

-------------------------icon Position---------------------------------
HANDLE Hout=getstdhandle (Std_output_handle); Handle
SetConsoleCursorPosition (Hout,{y,x}); Go to the X, y, and y, and note that X and Y are in reverse, written as {y,x}

-------------------------Clear Screen-------------------------------------
System ("CLS");

-------------------------wait-------------------------------------
Sleep (x); Wait x milliseconds

-----------------------key to determine-----------------------------------
Key_down (c); Judging the button press, the screen does not display

Determine whether to press C
The return value is 0 or 1, press 1, otherwise 0
Note: is to determine whether to press C, not to get the key pressed letters, if you judge the number, you must press the number of the row above
Can not use the keypad, F1, small keyboard, shift and other keys to use the Virtual Key Code table, the following one will be used.

#define Key_down (Vk_noname) ((Getasynckeystate (vk_noname) & 0x8000)? 1:0) Required header files
#include <windows.h> required header files

int b;
while (1==1) dead loop
{
b=0;
B=key_down (' 1 '); To determine the button press
if (b) cout<<1; If the key press is 1, the output 1
}


Judging by the Press

←↓→

#include <stdio.h>
#include <conio.h>
int main ()
{
int ch;
while ((Ch=getch ())!=0x1b) Press ESC to exit,
{
Switch (CH)
{
Case 0XE0:
Switch (Ch=getch ())
{
Case 72:printf ("up\n"); Break
Case 80:printf ("down\n"); Break
Case 75:printf ("left\n"); Break
Case 77:printf ("righr\n"); Break
Default:break;
}
Break
Default:break;
}
}
}
Program Description: ESC's virtual key table is 0x1b, enter enter for 13,↑ to 72,↓ for 80,← to 75,→ for 77
If you press ESC to exit if you press ↑, output up,↓, output down,←, output left,→, output righr
Recommendation: Write it as a function to change the output of printf 4 rows to call other functions, return values, etc.

Upgrade version:
int F;
F=getch ();
if (f==224)
F=getch ();
When f=224, that is, press ↑↓←→ and then take the F
F Common values:
Enter
72↑
80↓
75←
77→

---------------------------The calling program--------------------------------
WinExec (file path, 1);
File path \ To be changed to//, such as: I want to call D:\a\b\c.exe, will be written D://a//b//c.exe
If you want to open an. exe file in the same folder as the running program, but do not know the location of the file, or know the location of the file, the \//is a bit difficult, can be written C.exe,
If you want to open an. exe file under one or more folder directories under the folder where the program is running, but do not know the location of the file, or know the location of the file, the \//is a bit difficult, can be written as A//b//c.exe.

-------------------------get the file location------------------------------
GetCurrentDirectory (1000,BUF);

---------------------------Header File Set--------------------------------
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <conio.h>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <windows.h>
#define Key_down (Vk_noname) ((Getasynckeystate (vk_noname) & 0x8000)? 1:0)

--------------------------------function Declaration---------------------------
int a (); function declaration
int B (); function declaration
int A ()
{
b (); Call the B function, because the function declares that all can call B
}
int B ()
{
A (); Call a function
}

--------------------------------Program Calls---------------------------
WinExec ("X://xx//xxx.exe", f);
"" is the path, F is the passed-in value
Example: WinExec ("d://guess number//guess number//guess Number.exe", 1);
If you want to invoke files within a file or folder in your own directory:
WinExec ("Xxx.exe", f);
WinExec ("Xx//xxx.exe", f);
Cases:
WinExec ("Guess number.exe", 1);
WinExec ("Guess number//guess Number.exe", 1);

--------------------------------program Window---------------------------
System ("title timer"); Change the title and change the program name in Task Manager at the same time
System ("mode con cols=x lines=y"); x row y column, non-pixel point

HWND Hdos=getforegroundwindow (); Hide, show handle to Window
ShowWindow (Hdos,sw_hide); Hide Window
ShowWindow (hdos,sw_show); Display window

C + + Console programming (not yet tidy up the garbage)

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.