Chinaunix首页 | 论坛 | 博客
  • 博客访问: 57271
  • 博文数量: 21
  • 博客积分: 1440
  • 博客等级: 上尉
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-25 14:47
文章分类

全部博文(21)

文章存档

2010年(9)

2009年(12)

我的朋友

分类: C/C++

2009-09-05 23:21:58

学习的过程,注重的是思维和逻辑的过程。
文件I/O:
1.基于流的I/O;
2.基于文件描述符的I/O。
这里首先谈谈基于流的I/O:
what is stream?
A stream is a logical entity that represents a file or device that can accept input or output. All input and output functions in standard C operate on data streams.Streams can be devided into text streams and binary streams,it maybe also have an orientation towards byte I/O or wide I/O.
 
when you want to do input or output to a file,you have a choice of two basic mechanisms for representing the connection between your program and the file.I/O mechanisms can be devided into two parts:
1.file descriptors
2.streams
streams facilitate a way to create a level of abstraction between the program and an input or output device.
file(file.txt)<-stream<-program(*.c)
 
How to represents a streams?
For historical reasons,the type of the C data struct that represent a stream is called FILE rather than "stream".Since most of the library functions deal with objects of type FILE *,sometimes the term file pointer is also used to mean "stream".This leads to unfortunate confusion over terminology in many books onc.This manual,however is careful to use the term "file" and "stream" only in the technical sense.
 
FILE类型声明在头文件stdio.h中。
I.打开一个流的声明:
FILE *fopen(const char *filename,const char *opentype);
FILE *freopen(const char *filename,const char *opentpe,FILE *stream);
int _fwitable(FILE *stream);//是否可写
int _freadable(FILE *stream);//是否可读
opentype:
'r' 'w' 'a' 'r+' 'w+' 'a+' 'rw' 'ra' ...
 
II.关闭流的声明
int close(FILE *stream);
tips:closeing stream is that the connection to the corresponding file to be broken.Any buffer output is writen and any buffer input is discards.
 
file<-input buffer(stream)<-program(*.c)->output buffer(stream)->file 
 
III.Simple output by charactor or lines
int fputc(int c,FILE *stream);
int putc(int c,FILE *stream);
int putchar(int c);
int fputs(const char *s,FILE *stream);
int puts(const char *s);
 
IV.Get charactors from stream by charactor or lines
int fgetc(FILE *stream);
int getc(FILE *stream);//a macro
ssize_t getline(char **lineptr,size_t *n,FILE *stream);
char *fgets(char *s,int count,FILE *stream);
char *gets(char *s);
size_t 为sizeof返回值的类型,unsigned integer
ssize_t this datatype is used to represent the sizes of block that can be read or written in a single operator,it is similar to size_t,but be a signed type.
 
BLOCK INPUT AND BLOCK OUTPUT
size_t fread(void *data,size_t size,size_t count,FILE *stream);
The function reads up to count objects of size into the array data,from the stream stream.
 
size_t fwrite(const void *data,size_t size,size_t count,FILE *stream);
 
VI.测试是否到达文件尾部
int feof(FILE *stream);//常用判断
if EOF(macro):end of file,declared in stdio.h
 
VII.几个有用的声明
int ferror(FILE *stream);
void clearerr(FILE *stream);
long int ftell(FILE *stream);//返回文件当前位置
off_t ftello(FILE *stream);
int fseek(FILE *stream,long int offet,int whence);//设定文件的当前位置,很有用
whence:SEEK_SET,SEEK_CUR,SEEK_END
int fflush(FILE *stream);
int setvbuf(FILE *stream,char *buf,int mode,size_t size);
void setbut(FILE *stream,char *buf);
void setbuffer(FILE *stream,char *buf,size_t size);
 
以上是基于流的文件I/O,很多都只是提出来,因为学校对于I/O的讲解少,很多人根本不知道I/O的一些很普遍要学要知道的功能和应用,具体的这些函数的使用,就要自己去查相关资料,想把I/O学好的C爱好者,最好自己去实际操作,用例子来说明每个函数的作用,这样才能在遇到I/O时游刃有余,从恐惧到笑对I/O。有时间我会将自己一起做的一些例子写到这里,让大家感受I/O的魅力。
 
tips:无知的人才会说I/O简单,切忌浮躁,务必踏实
 
阅读(708) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~