单字符I/O:getchar()和putchar()

 #include<stdio.h>

 int main(void)
{
char ch;
while ((ch = getchar()) != '#')
putchar(ch);
return ;
}
 #include<stdio.h>

 int main(void)
{
char ch;
while ((ch = getchar()) != EOF) //EOF是代表文件结尾的标志
putchar(ch);
return ;
}

重定向和文件

重定向输入让程序使用文件而不是键盘来输入,重定向输出让程序输出至文件而不是屏幕。

1、重定向输入

假设已经编译了echo_eof.c程序,并把可执行版本放入一个名为echo_eof的文件中。运行该程序,输入可执行文件名:

echo_eof < words

2、 重定向输出

echo_eof > words

3、组合重定向

假设希望制作一份mywords文件的副本,并命令为savewords。只需要输入:

echo_eof<mywords>savewords
echo_eof>mywords<savewords

注意:重定向运算符连接一个可执行程序和一个数据文件;且不能读取多个文件的输入,也不能把输出定向至多个文件。

创建更友好的用户界面

#include<stdio.h>
void display(char cr, int lines, int width); int main(void)
{
int ch;
int rows, cols;
printf("Enter a character and two integers:\n");
while ((ch = getchar()) != '\n')
{
if (scanf_s("%d %d", &rows, &cols) != )
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 ;
} void display(char cr, int lines, int width)
{
int row, col;
for (row = ; row <= lines; row++)
{
for (col = ; col <= width; col++)
putchar(cr);
putchar('\n');
}
}

输入由字符组成,但是scanf()函数可以使用说明符把输入转换成相应的类型。

例子

 #include<stdio.h>
char get_choice(void);
void count(void);
char get_first(void);
int get_int(void); int main(void)
{
int choice;
while ((choice = get_choice()) != 'q') {
switch (choice)
{
case 'a':printf("Buy low, sell high.\n");
break;
case 'b':printf("OK");
break;
case 'c':count();
break;
default: printf("Program error!\n");
break;
}
}
return ;
} char get_choice(void)
{
int ch;
printf("Enter the letter of your choice:\n");
printf("a. advice b. state\n");
printf("c. count q. quit\n");
ch = get_first;
while ((ch <'a' || ch>'c') && ch != 'q') {
printf("Please response with a, b, c, or q.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
int get_int(void)
{
int input;
char ch;
while (scanf_s("%d", &input)!=) {
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an integer.\nPlease enter an integer value,such as 25.");
}
return input;
}
void count(void)
{
int n, i;
printf("Count how far?Enter an integer:\n");
n = get_int();
for (i = ; i < n; i++)
printf("%d\n", i);
while (getchar() != '\n')
continue;
}

c语言之字符输入输出和输入验证的更多相关文章

  1. C语言中字符数据的输入和输出

    字符的输出 C语言中使用putchar函数来输出字符数据 #include <stdio.h> int main() { char a,b,c,d; //定义字符变量a,b,c,d a = ...

  2. C Primer Plus_第8章_字符输入输出和输入确认_编程练习

    1.题略 #include <stdio.h> int main(void) { ; printf("Please enter text here(end with Ctrl + ...

  3. C学习笔记(八)字符输入输出和输入确认

    缓冲区 缓冲区分为两类:完全缓冲(fully buffered)I/O和行缓冲(line-buffered)I/O.完全缓冲在缓冲区满时被清空(内容被发送至目的地).这种类型常出现在文件输入中.缓冲区 ...

  4. Ubuntu下键盘输入错乱问题,输入双引号输出的是@符号,输入#号输出的是未知语言的字符

    装完搜狗后,键盘开始出现混乱,切换到英文输入法,输入双引号输出的是@符号,输入#号输出的是未知语言的字符. 网上有的说在 system - keyboard - Input Source 下看看是否是 ...

  5. C语言-逃逸字符、类型转换和布尔类型

    C语言-逃逸字符 逃逸字符是用来表达无法印出来的控制字符或者特殊字符,它由一个反斜杠""开头,后面跟上另一个字符,这两个字符合起来,组成一个字符. \b是backspace,在su ...

  6. Struts2入门(四)——数据输入验证

    一.前言 1.1.什么是输入验证?为什么需要输入验证? 在上一篇文章中,我们学习了数据类型转换,我们提到了表示层数据处理的两个方法,也提到了用户输入数据需要进行类型转换才能得到我们想要的数据,那么,我 ...

  7. javascript设计模式实践之策略模式--输入验证

    策略模式中的策略就是一种算法或者业务规则,将这些策略作为函数进行封装,并向外提供统一的调用执行. 先定义一个简单的输入表单: <!DOCTYPE html> <html> &l ...

  8. C语言的字符测试函数

    C语言的字符测试函数 isalnum, isalpha, isdigit, isxdigit, isblank, isspace, isascii, iscntrl, ispunct, isgraph ...

  9. Struts2的输入验证

    一.概述: ① Struts2的输入验证 –基于 XWorkValidation Framework的声明式验证:Struts2提供了一些基于 XWork Validation Framework的内 ...

随机推荐

  1. ArcGIS Server10.2 集群部署注意事项

    不接触Server很久了,最近一个省级项目需要提交一个部署方案,由于是省级系统,数据.服务数量都较大,需要考虑采用Server集群的方式来实现.在网上搜罗了以下Server集群的资料,按照步骤一步步来 ...

  2. Scrapy(爬虫框架)中,Spider类中parse()方法的工作机制

    parse(self,response):当请求url返回网页没有指定回调函数,默认的Request对象的回调函数,用来处理网页返回的response,和生成的Item或者Request对象 以下分析 ...

  3. python创建列表和向列表添加元素方法

    今天的学习内容是python中的列表的相关内容. 一.创建列表 1.创建一个普通列表 >>> tabulation1 = ['大圣','天蓬','卷帘'] >>> ...

  4. May 24. 2018 Week 21st Thursday

    Man errs so long as he strives. 失误是进取的代价. It is not important that the man in the arena didn't win, ...

  5. kafka-connect-hdfs连接hadoop hdfs时候,竟然是单点的,太可怕了。。。果断改成HA

    2017-08-16 11:57:28,237 WARN [org.apache.hadoop.hdfs.LeaseRenewer][458] - <Failed to renew lease ...

  6. vi/vim tab键空格数修改

    更改Tap键单位 vi/vim编辑器默认情况下,每按一次Tap相对于8个空格. (1)临时性更改 使用vi打开文件后,输入如下命令: :set tabstop=4 命令释义:更改为相当于四个空格. ( ...

  7. 彻底理解js中的&&和||

    阅读代码时对一行代码很困惑 step > max_step && (step = min_step); 查阅资料后发现它等价于 if (step > max_step) { ...

  8. C#自动化操作IE浏览器系列之一打开新的浏览器并导航到百度进行搜索

    现在OA系统.生产管理系统都是B/S结构,很多的办公室工作都要用浏览器操作.自己会一点C# winform开发,就做个小工具自动化处理一些繁琐的操作. 我的开发环境是:win7旗舰版+VS2010(或 ...

  9. Problem UVA11134-Fabled Rooks(贪心)

    Problem UVA11134-Fabled Rooks Accept: 716  Submit: 6134Time Limit: 3000mSec Problem Description We w ...

  10. 解决Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.

    异常: Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj ...