|
因本人不再做技术,
这个blog将不再连载技术文章,
只作为心情点滴的记录,
想学技术的请绕道,谢谢!
联系方式:
feiyu_lili@163.com |
时 间 记 忆 |
« | July 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | |
|
blog名称:飞鱼的成长 日志总数:120 评论数量:488 留言数量:18 访问次数:1043294 建立时间:2006年2月27日 |
 | | | |
|
|
重定向
一:输入定向
二:输出定向
三:组合重定向
创建一个更友好的用户界面:
/* showchar1.c -- program with a BIG I/O problem */
int main(void)
{
int ch; /* character to be printed */
int rows, cols; /* number of rows and columns */
printf("Enter a character and two integers:\n");
while ((ch = getchar()) != '\n')
{
scanf("%d %d", &rows, &cols);
display(ch, rows, cols);
printf("Enter another character and two integers;\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");
return 0;
}
void display(char cr, int lines, int width)
{
int row, col;
for (row = 1; row <= lines; row++)
{
for (col = 1; col <= width; col++)
putchar(cr);
putchar('\n'); /* end line and start a new one */
}
}
500)this.width=500'>
这里输入了c 1 2一次循环以后就直接跳出了,原因与scanf()不同getchar()并不跳过换行符(还有空格,制表符),所以在输入c 1 2后面的换行符就被留在输入队列中,在下次循环的时候没有机会读取任何数据,这一个换行符由getchar()读出,负值给ch而ch为换行符正式终止循环条件。
所以就没有按照我们所想的退出程序。
下面是修改后的程序
/* showchar2.c -- prints characters in rows and columns */
#include <stdio.h>
void display(char cr, int lines, int width);
int main(void)
{
int ch; /* character to be printed */
int rows, cols; /* number of rows and columns */
printf("Enter a character and two integers:\n");
while ((ch = getchar()) != '\n')
//While语句使程序剔除scanf()输入后的所有字符,包括换行符。这样就让循环准备好读取下一//行开始的第一个字符。其中scanf()返回值不是2,就终止了该程序。
{
if (scanf("%d %d",&rows, &cols) != 2)
break;
display(ch, rows, cols);
while (getchar() != '\n')
continue;
printf("Enter another character and two integers;\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");
return 0;
}
void display(char cr, int lines, int width)
{
int row, col;
for (row = 1; row <= lines; row++)
{
for (col = 1; col <= width; col++)
putchar(cr);
putchar('\n'); /* end line and start a new one */
}
}
500)this.width=500'> |
|
| | |
|