c语言之字符输入输出和输入验证
单字符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语言之字符输入输出和输入验证的更多相关文章
- C语言中字符数据的输入和输出
字符的输出 C语言中使用putchar函数来输出字符数据 #include <stdio.h> int main() { char a,b,c,d; //定义字符变量a,b,c,d a = ...
- C Primer Plus_第8章_字符输入输出和输入确认_编程练习
1.题略 #include <stdio.h> int main(void) { ; printf("Please enter text here(end with Ctrl + ...
- C学习笔记(八)字符输入输出和输入确认
缓冲区 缓冲区分为两类:完全缓冲(fully buffered)I/O和行缓冲(line-buffered)I/O.完全缓冲在缓冲区满时被清空(内容被发送至目的地).这种类型常出现在文件输入中.缓冲区 ...
- Ubuntu下键盘输入错乱问题,输入双引号输出的是@符号,输入#号输出的是未知语言的字符
装完搜狗后,键盘开始出现混乱,切换到英文输入法,输入双引号输出的是@符号,输入#号输出的是未知语言的字符. 网上有的说在 system - keyboard - Input Source 下看看是否是 ...
- C语言-逃逸字符、类型转换和布尔类型
C语言-逃逸字符 逃逸字符是用来表达无法印出来的控制字符或者特殊字符,它由一个反斜杠""开头,后面跟上另一个字符,这两个字符合起来,组成一个字符. \b是backspace,在su ...
- Struts2入门(四)——数据输入验证
一.前言 1.1.什么是输入验证?为什么需要输入验证? 在上一篇文章中,我们学习了数据类型转换,我们提到了表示层数据处理的两个方法,也提到了用户输入数据需要进行类型转换才能得到我们想要的数据,那么,我 ...
- javascript设计模式实践之策略模式--输入验证
策略模式中的策略就是一种算法或者业务规则,将这些策略作为函数进行封装,并向外提供统一的调用执行. 先定义一个简单的输入表单: <!DOCTYPE html> <html> &l ...
- C语言的字符测试函数
C语言的字符测试函数 isalnum, isalpha, isdigit, isxdigit, isblank, isspace, isascii, iscntrl, ispunct, isgraph ...
- Struts2的输入验证
一.概述: ① Struts2的输入验证 –基于 XWorkValidation Framework的声明式验证:Struts2提供了一些基于 XWork Validation Framework的内 ...
随机推荐
- Not Found woff 字体库
原因是,默认在IIS上是没有添加对*.woff,*.svg文件的Mime类型,因此在客户端请求此类文件时得到的都是404. 所以我们只需要在我们对应网站下的Mime类型中添加文件对应的类型就行了 .w ...
- Session变量在PHP中的使用
PHP session 变量用于存储有关用户会话的信息,或更改用户会话的设置.Session 变量保存的信息是单一用户的,并且可供应用程序中的所有页面使用. PHP Session 变量 当您运行一个 ...
- ASP.NET -- WebForm -- Cookie的使用
ASP.NET -- WebForm -- Cookie的使用 Cookie是存在浏览器内存或磁盘上. 1. Test3.aspx文件 <%@ Page Language="C#&q ...
- February 14th, 2018 Week 7th Wednesday
Love does not dominate, it culitvates. 爱不是羁绊,而是成就. Love should not wipe out everything you are, love ...
- 怎样用命令行开启或关闭Windows服务
怎样用命令行开启或关闭Windows服务 本篇博客主要包含一个内容: 怎样用命令行开启或关闭Windows服务 闲话少叙,直奔主题. 1.在桌面右击新建一个文本文档,然后打开. 2.找到需要开启或关闭 ...
- 关于reduce的理解
什么是reduce reduce这个词字面上来讲,大多称作“归约”,但这个词太专业了,以至于第一眼看不出来意思.我更倾向于解释为“塌缩”,这样就形象多了.对一个n维的情况进行reduce,就是将执行操 ...
- php 10进制转62进制,可用于短网址生成
<?php /** * 十进制数转换成62进制 * * @param integer $num * @return string */ function from10_to62($num) { ...
- Python:Day05 作业
购物车: product_list = [['iphone6s',5800],['mac book',9800],['coffee',32],['book',80],['bike',1500]] sh ...
- day05 数据基本类型及内置方法:字符串str、列表list
一:可变不可变类型 1.可变类型 值改变,id不变,说明是直接改变原值,是可变类型 2.不可变类型 值改变,id也跟着改变,说明是产生了新的值,是不可变类型 二:数字类型 1.整型Int 用途: 记录 ...
- mac python3 conda pytorch出错:libc++abi.dylib: terminating with uncaught exception of type NSException
mac 10.14/ conda/python 3.7环境下运行神经网络例子出现错误: -- :::] -[NSApplication _setup:]: unrecognized selector ...