C语言——输入输出
2016-05-02 00:00
246 查看
摘要: C语言 输入输出
从函数学起
①标准的输入输出
//输入来自键盘字符串遇到‘\n’结束,来自文件则EOF结束。[/code]
使用:
从函数学起
①标准的输入输出
int getchar (void) [code=cpp]//头文件: stdio.h //函数参数:无 //函数返回值:成功返回字符串,失败或者读到结束符返回EOF(-1) //函数功能:在标准输入上读到一个字符//从标准输入输出读取一个字符,不限于键盘
//输入来自键盘字符串遇到‘\n’结束,来自文件则EOF结束。[/code]
int putchar (int c) //头文件: stdio.h //函数参数:c为字符串常量或者表达式 //函数返回值:输出的字符 //函数功能:在标准输出上显示一个字符使用:
#include<stdio.h> #include<ctype.h> main() { int c; while((c=getchar())!=EOF) putchar(c); return 0; }②格式化输入输出
int scanf(const char*format ,...) //头文件: stdio.h //函数参数: formatz指定输入的格式,后面跟输入变量的地址表,不定参 //函数返回值:成功返回个数,失败EOF //函数功能:格式化输入
int printf (char * format ,arg1,arg2....) //头文件: stdio.h //函数参数:format指定输出格式,后面跟输出的变量,不定参。 //函数返回值:成功返回输出的字节数,失败返回EOF(-1) //函数功能:格式化字符串输出
使用:
#include<stdio.h> main() { int yy,mm,dd; printf("输入年月日:(如:19900412)"); scanf("%4d%2d%2d",&yy,&mm,&dd); printf("%4d 年, %2d 月,%2d 日",yy,mm,dd); }③字符串的输入输出函数
int puts(const char *s) //功能:在标准输出上显示字符串 //参数:s是要显示的字符串 //返回值:成功则返回一个非零值,失败返回EOF
char *gets(char *s) //功能:从键盘输入一回车结束符放入字符数组中,并且自动加‘\0’ //参数:s为字符串数组,存储输入的字符串 //返回值:成功返回字符串的起始地址,失败返回NULL
#include <stdio.h> #include <stdlib.h> int my_strlen(char *s) { int n; while(*(s++)!='\0')n++; return n; } char tab[100]; char *temp; int main() { temp=tab; while(((*(temp++))=getchar())!='\n'); *temp='\n'; puts(tab); printf("%d",my_strlen(tab)); return 0; }④文件的访问
FILE *fp; FILE *fopen(char *name ,char * mode); //fp 是FILE类型的结构指针 // name 文件名 mode 访问模式
//调用文件 fp=fopen(name,mode);
int getc(FILE *fp) int putc(int c,FILE *fp) #define getchar getc(stdin) #define putchar putc((c),stdout)
int fscanf(FILE *fp ,char * format ,...) int fprintf(FILE *fp,char * format ,...)
int fclose (FILE *fp)
相关文章推荐
- C/C++的字符串流
- linux重定向详解
- 带缓存的IO和不带缓存的IO
- 8,10,16进制输入与输出
- 重定向子进程控制台程序的输入输出
- 解读一条SHELL语句
- 标准输入输出和管道
- C语言中自带的头文件(.h)所包含的函数
- python标准输入输出
- C/C++ 标准输入输出重定向
- C++标准输入输出格式控制(进制)
- 标准输入输出、错误输出、重定向标准输出
- Shell重定向&>file、2>&1、1>&2的区别
- Linux 标准输入输出详解
- 初识别输入输出
- 使用VScode进行C/C++开发
- 在C++中实现字符串分割--split
- 便利的开发工具 CppUnit 快速使用指南
- 用CPPUnit做单元测试
- 【AKOJ】1114-水仙花数