标准I/O目录
1.标准I/O介绍
- 标准I/O在系统调用(文件I/O)的基础上构造,便于用户使用,但相对与系统调用多了缓冲机制。
2.文件缓冲
-
文件系统给有两类:带缓冲文件系统(高级磁盘IO)和 非缓冲文件系统(低级磁盘IO)。
-
缓冲文件系统是为了减少 系统调用(system call)read write的调用频率,降低操作系统负担。
-
缓冲文件系统,系统自动在内存中为每一个正在使用的文件开辟一个缓冲区,从内存向磁盘输出数据或从磁盘读取数据时,都是先放到缓冲区。
-
缓冲文件系统又分为,全缓冲,行缓冲,不缓冲三种
-
使用setbuf()和setvbuf() 可以更改缓冲的类型,而且在任何时刻都可以使用fflush强制刷新一个数据流。
3.常用函数
3.1 fopen()
函数原型 | FILE *fopen(const char *path,const char *mode) |
---|---|
函数功能 | 打开一个由path指定的文件 |
函数参数 | path:文件路径 mode: 打开方式 “w+” “a+” "r+"等 |
函数返回 | 成功返回一个FILE *类型的文件指针 失败返回 NULL |
FILE *fopen(const char *path,const char *mode)
//例子
int main()
{
FILE *fp;
if((fp = fopen("3.txt","w+"))==NULL)
{
perror("fopen");
return -1;
}
}
3.2 fclose()
函数原型 | int fclose( FILE *fp ) |
---|---|
函数功能 | 关闭一个流 |
函数参数 | fp:文件标识符 |
函数返回 | 成功返回 0 失败返回EOF(-1) |
3.3 fgetc()
函数原型 | int fgetc(FILE *stream) |
---|---|
函数功能 | 从流中读取一个字符 |
函数参数 | stream:流标识符 |
函数返回 | 成功返回字符 失败或者已经处于文件尾部返回EOF(-1) |
//例子
#include <stdio.h>
int main(int argc, char *argv[])
{
int a = getc(stdin);//从标准输入读取一个字符
printf("a = %d, %c\n", a, a);
return 0;
}
3.4 fputc()
函数原型 | int fputc(int c,FILE *stream) |
---|---|
函数功能 | 向流中输入一个字符 |
函数参数 | stream:流标识符 |
函数返回 | 成功返回一个非负值 失败返回EOF(-1) |
//例子
#include <stdio.h>
int main(int argc, char *argv[])
{
/*打开一个文件*/
FILE *fp = fopen("./10.txt", "a+");
if (NULL == fp){
perror("fopen");
return -1;
}
/*从标准输入读取字符,输出到文件中*/
while(1){
int a = fgetc(stdin);
if (a == '\n')
break;
fputc(a, fp);
}
return 0;
}
3.5 fgets()
函数原型 | char *fgets(char *s,int size,FILE *stream) |
---|---|
函数功能 | 从流中读取一行字符到s指向的地址中 |
函数参数 | s: 指向缓冲区的指针 size: 缓冲区大小(一次最大读取字节数,一行大小超过size时,分多次读取一行) |
函数返回 | 成功:返回buf 失败或者已处于文件尾端:返回NULL(用feof()/ferror()来区分是处于末位还是出错) |
char *fgets(char *s,int size,FILE *stream);
//例子 fgets()计算文件行数
#include<stdio.h>
int main()
{
int line = 0;
char buf[32];
FILE *fp = fopen("./11.txt", "a+");
if (NULL == fp){
perror("fopen");
return -1;
}
while(fgets(buf,32,fp)!=NULL)
{
if(buf[strlen(buf)-1] == '\n')
{
line ++;
}
}
return 0;
}
3.6 fputs()
函数原型 | int fputs(const char *s,FILE *stream) |
---|---|
函数功能 | 向流中输入一行字符(以null结尾) |
函数参数 | s:指向缓冲区的指针 stream:流标识符 |
函数返回 | 成功:非负值 失败:EOF(-1) |
//例子
#include<stdio.h>
#difine MAXLINE 20
int main()
{
char line[MAXLINE];
while(fgets(line,MAXLINE,stdin)!=NULL && line[0]!='\n')
{
fputs("result:",stdout);
fputs(line,stdout);
}
return 0;
}
3.7 fread()/fwrite()
函数原型 | size_t fread(void *ptr,size_t size,size_t nmemb,FILE *stream) size_t fwrite(void *ptr,size_t size,size_t nmemb,FILE *stream) |
---|---|
函数功能 | 读取/输入特定个数的对象 |
函数参数 | ptr:指向缓冲区的指针 size:读取对象的大小 nmemb:一次读取对象的个数 stream:流标识符 |
函数返回 | 读或者写的对象数 |
//例子:将当前时间不断的输入到文件中,间隔1s
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>
#include<time.h>
#include<unistd.h>
int main()
{
FILE *fp = fopen("time.txt","a+");
if(NULL == fp)
{
perror("fopen");
return -1;
}
char buf[32];
int count = 1;
int c;
/* 计算文件原有的行数 */
while((c = fread(buf,sizeof(char),32,fp)) > 0 )
{
int i;
for(i = 0;i < c;i++)
{
if(buf[i] == '\n')
{
count++;
}
}
}
/*设置文件位置指示到文件尾部 */
fseek(fp,0,SEEK_END);
struct tm* m_time; //时间结构体
time_t now;
while(1)
{
time(&now); //计算现在到1900年1月1日经过的秒数
m_time = localtime(&now);//计算本地时间
sprintf(buf,"%d:%d-%d-%d %d:%d:%d \n",count++,1900+m_time->tm_year,1+m_time->tm_mon,m_time->tm_mday,m_time->tm_hour,m_time->tm_min,m_time->tm_sec);
fputs(buf,fp);
fflush(fp);
sleep(1);//休眠1s
}
}
3.8 fseek()
函数原型 | int fseek(FILE *stream,long offset,int whence) |
---|---|
函数功能 | 设置文件位置指示 |
函数参数 | stream:流标识符 offset: 偏移(向前为负,向后为正) whence:基点 SEEK_SET文件头 SEEK_CUR当前 SEEK_END文件尾 |
函数返回 | 成功:0 失败:-1 |
3.9 ftell()
函数原型 | long ftell(FILE *stream) |
---|---|
函数功能 | 取得文件位置指示 |
函数参数 | stream:流标识符 |
函数返回 | 成功:当前文件指示位置 失败:-1L |