例315:
#include <conio.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <stdlib.h>
#include <dos.h>
int main(int argc, char const *argv[])
{
char buffer[128];
int row, column;
clrscr();
cprintf("Jamsa's 1001 C/C++ Tips\r\n");
gettext(1, 1, 23, 1, buffer);
while (!kbhit())
{
clrscr();
row = 1 + random(24);
column = 1 + random(58);
puttext(column, row, column+22, row, buffer);
delay(2000);
}
return 0;
}
例316:
#include <conio.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
int main(int argc, char const *argv[])
{
char buffer[8000];
int handle;
if ((handle = creat("SAVESCR.DAT", S_IWRITE)) == -1)
cprintf("Error opening SAVESCRN.DAT\r\n");
else
{
gettext(1, 1, 80, 25, buffer);
write(handle, buffer, sizeof(buffer));
close(handle);
}
return 0;
}
例317:
#include <conio.h>
int main(int argc, char const *argv[])
{
union TextColor {
struct {
unsigned char foreground:4;
unsigned char background:3;
unsigned char blinking:1;
} color_bits;
unsigned char value;
} colors;
colors.color_bits.foreground = BLUE;
colors.color_bits.background = RED;
colors.color_bits.blinking = 1;
textattr(colors.value);
clrscr();
cprintf("This is the new text colors\n");
return 0;
}
例318:
#include <conio.h>
int main(int argc, char const *argv[])
{
int color;
for (color = 1; color < 16; color++)
{
textattr(color);
cprintf("This is color %d\r\n", color);
}
textattr(128 + 15);
cprintf("This is blinking\r\n");
return 0;
}
例319:
#include <conio.h>
int main(int argc, char const *argv[])
{
struct text_info text;
gettextinfo(&text);
cprintf("Screen coordinates %d,%d to %d,%d\r\n", text.wintop, text.winleft, text.winbottom, text.winright);
cprintf("Text attribute %d Normal attribute %d\r\n", text.attribute, text.normattr);
cprintf("Screen height %d width %d\r\n", text.screenheight, text.screenwidth);
cprintf("Cursor position was row %d column %d\r\n", text.cury, text.curx);
return 0;
}
例320:
#include <conio.h>
int main(int argc, char const *argv[])
{
int color;
for (color = 1; color < 16; color++)
{
textcolor(color);
cprintf("This is color %d\r\n", color);
}
textcolor(128 + 15);
cprintf("This is blinking\r\n");
return 0;
}
例321:输入大写字母就换行输出
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int main(int argc, char const *argv[])
{
int letter;
int done = 0;
int uppercase_found = 0;
do {
letter = getch();
if (islower(letter))
putchar(letter);
else
{
if (isupper(letter))
{
ungetch(letter);
uppercase_found = 1;
putchar('\n');
}
done = 1;
}
} while (! done);
if (uppercase_found)
do {
letter = getch();
putchar(letter);
} while (letter != '\r');
return 0;
}
例322:打印文字后输出光标位置
#include <conio.h>
int main(int argc, char const *argv[])
{
int row, column;
clrscr();
cprintf("This is line 1\r\n");
cprintf("Line 2 is a little longer\r\n");
cprintf("This is the last line");
// 记录光标位置
row = wherey();
column = wherex();
cprintf("\r\nThe cursor position was row %d column %d\n", row, column);
return 0;
}
例323:设置窗口位置
#include <conio.h>
int main(int argc, char const *argv[])
{
int i, j;
// 前两个参数是左上角的坐标,也就是窗口的起点,后两个分别是长和宽 - tc可用
window(1, 1, 40, 12);
for (i = 0; i < 15; i++)
{
for (j = 0; j < 50; j++)
cprintf("%d", j);
cprintf("\r\n");
}
return 0;
}
例324:源文件的行号改变宏定义
#include <stdio.h>
int main(int argc, char const *argv[])
{
// __LINE__ :当前程序行的行号,表示为十进制整型常量
// __FILE__ :当前源文件名,表示字符串型常量
printf("File %s: Successfully reached line %d\n", __FILE__, __LINE__);// File CHG_LINE.C: Successfully reached line 7
// 指定下一行的__LINE__为100,__FILE__为FILENAME.C,重点是line的下一行是200
#line 100 "FILENAME.C"
printf("File %s: Successfully reached line %d\n", __FILE__, __LINE__);// File FILENAME.C: Successfully reached line 101
return 0;
}
例325:判断是否为标准c程序的预定义宏
#include <stdio.h>
int main(int argc, char const *argv[])
{
// 判断该文件是不是标准C程序
#ifdef __STDC__
printf("ANSI C compliance\n");
#else
printf("Not in ANSI C mode\n");
#endif
return 0;
}
例326:判断是否为标准c++的预定义宏
#include <stdio.h>
int main(int argc, char const *argv[])
{
// 判断是否是c++代码
#ifdef __cplusplus
printf("Using C++\n");
#else
printf("Using C\n");
#endif
return 0;
}
例327:源文件名预定义宏__FILE__
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("The file %s is under Beta testing\n", __FILE__);
return 0;
}
例328:当前行号预定义宏__LINE__
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Successfully reached line %d\n", __LINE__); // Successfully reached line 5
// Other statements here
printf("Successfully reached line %d\n", __LINE__); // Successfully reached line 9
return 0;
}