控制流
分支
循环
跳转
I/O流
1 printf()与scanf ()
#include <stdio.h>
int main()
{
int a, b, c;
scanf("%d%d", &a, &b); /* scanf中有2个格式说明符,表明要取两次数据,取走10和20
缓冲区中还剩下30和40
*/
printf("a=%d b=%d \n", a, b);
scanf("%d", &c); /* scanf中只有1个格式说明符,表明要取1次数据,取走30
缓冲区中还剩下40,因此,读者还可以取一次。。。。。
*/
printf("c=%d \n", c);
return 0;
}
2 getchar()与putchar()
#include <stdio.h>
int main()
{
char c, s[20];
printf("Enter a string: ");
c = getchar();
printf("Read the remaining from the buffer \n");
scanf("%s", s);
putchar(c);
putchar('\n');
printf("%s \n", s);
}
3 gets()与puts()
#include <stdio.h>
int main()
{
char s[50];
printf("Enter a string: ");
gets(s);
printf("The string you entered: ");
puts(s);
}
4 getche()与getch()
#include <stdio.h>
#include <conio.h> /* getche(), getch() */
int main()
{
char c1, c2;
printf("Press any key to exit ");
c1 = getche();
putchar('\n');
printf("Press any key once more to exit ");
c2 = getch();
putchar('\n');
printf("The character getche() read: %c \n", c1);
printf("The character getch() read: %c \n", c2);
}
5 getc()和putc()
另外还有一组输入输出函数是getc()和putc(),它们的作用也是输入和输出一个字符,先看它们在头文件stdio.h中的定义。
以下是头文件stdio.h的部分内容,请注意注释部分:
/* size_t的定义 */
#ifndef _SIZE_T_DEFINED
typedef unsigned int size_t;
#define _SIZE_T_DEFINED
#endif
#define EOF (-1) //EOF的定义
/* FILE结构的定义 */
#ifndef _FILE_DEFINED
struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
typedef struct _iobuf FILE;
#define _FILE_DEFINED
#endif
#define FILENAME_MAX 260 //最大文件名长度
#define FOPEN_MAX 20 //最多可打开的文件数
/* NULL指针值的定义 */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
/* 标准I/O设备数组_iob[]的定义 */
#ifndef _STDIO_DEFINED
_CRTIMP extern FILE _iob[];
#endif
/* 标准I/O设备的定义 */
#define stdin (&_iob[0])
#define stdout (&_iob[1])
#define stderr (&_iob[2])
/* 函数原型 */
_CRTIMP int __cdecl scanf(const char *, );
_CRTIMP int __cdecl getc(FILE *);
_CRTIMP int __cdecl getchar(void);
_CRTIMP char * __cdecl gets(char *);
_CRTIMP int __cdecl printf(const char *, );
_CRTIMP int __cdecl putc(int, FILE *);
_CRTIMP int __cdecl putchar(int);
_CRTIMP int __cdecl puts(const char *);
/* 宏定义 */
#define feof(_stream) ((_stream)->_flag & _IOEOF)
#define getc(_stream) (--(_stream)->_cnt >= 0 \
? 0xff & *(_stream)->_ptr++ : _filbuf(_stream))
#define putc(_c,_stream) (--(_stream)->_cnt >= 0 \
? 0xff & (*(_stream)->_ptr++ = (char)(_c)) : _flsbuf((_c),(_stream)))
#define getchar() getc(stdin) //调用getchar()时,实际调用的是getc(stdin)
#define putchar(_c) putc((_c),stdout) //调用putchar(_c)时,实际调用的是putc((_c), stdin)
文件流
有关文件的概念
按文件的逻辑结构:
记录文件:由具有一定结构的记录组成(定长和不定长)
流式文件:由一个个字符(字节)数据顺序组成
按存储介质:
普通文件:存储介质文件(磁盘、磁带等)
设备文件:非存储介质(键盘、显示器、打印机等)
按数据的组织形式:
文本文件: ASCII文件,每个字节存放一个字符的ASCII码
二进制文件:数据按其在内存中的存储形式原样存放
C语言文件指针
在C语言中用一个指针变量指向一个文件,这个指针称为文件指针。
声明FILE结构体类型的信息包含在头文件“stdio.h”中
一般设置一个指向FILE类型变量的指针变量,然后通过它来引用这些FILE类型变量 通过文件指针就可对它所指的文件进行各种操作。
定义说明文件指针的一般形式为:
FILE * 指针变量标识符;
其中FILE应为大写,它实际上是由系统定义的一个结构,该结构中含有文件名、文件状态和文件 当前位置等信息。在编写源程序时不必关心FILE结构的细节。
FILE *fp;
表示fp是指向FILE结构的指针变量,通过fp即可找存放某个文件信息的结构变量,然后按结构变量提供的信息找到该文件,实施对文件的操作。习惯上也笼统地把fp称为指向一个文件的指针。
希望关注点赞评论,传递知识
想要关注我一起学习的请关注公众号:莫林的日常
想要和志同道合的伙伴一起学习的请加QQ群:515271405