Chinaunix首页 | 论坛 | 博客
  • 博客访问: 333924
  • 博文数量: 92
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 960
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-21 19:38
文章分类

全部博文(92)

文章存档

2010年(71)

2009年(21)

我的朋友

分类: 嵌入式

2009-08-23 16:34:28

Reference: "Linux application development technology "
glibc library standard I/O-flow mechanism completes the operation on the document. Computer system provides two interfaces: system calls and library functions. Under normal circumstances, both the preparation of system libraries or applications, are inseparable from the I/O environment.
At the beginning of each running process three standard file pointers must be opened, that is, standard input (stdin), standard output (stdout), standard error (stderr).
 
1> open and close the flow
# include
FILE * fopen (const char * pathname, const char * type);
int fclose (FILE * stream);
pahtname for the file path and file name of the string, type is the way how to open, as follows:
r: file must already exist, in order to read and open.
r +: file must already exist, in order to read and write and open.
w: create a file or open a file for writing, there will be empty if the file contents is of the original document.
w +: create a file or to read and write and open the file, there will be empty if the file contents is of the original document.
a: additional, that is the end of the document by adding content, or for the writing and create a file.
a +: in the end to write the contents of a document, or documents to read and write and create.
For example:
 
 
2> read and write data blocks flow
# include
size_ fread (void * ptr, size_t size, size_t nobj, FILE * stream);
size_t fwrite (const void * ptr, size_t size, size_t nobj, FILE * stream);
ptr is a pointer to the cache area, size refers to the size of data blocks, nobj means read or write the number of data blocks, stream designated to operate the data stream.
 
3> read and write stream of characters
# include
int fgetc (FILE * stream);
int fputc (int c, FILE * stream)
For example:
while ((ch = fgetc (fp))! = EOF) / / read the characters in the document in clycle, and then output to terminal
      fputc (ch, stdout);
 
4> string write flow
# include
char * fgets (char * s, int size, FILE * stream);
int fputs (const char * s, FILE * stream);
For example:
FILE * fp;
fp = fopen ( "8-4.txt", "r");
fgets (str, sizeof (str), fp);
 
5> positioning flow
# include
void rewind (FILE * stream);
int fseek (FILE * stream, long offset, int whence);
long ftell (FILE * stream);
int fgetpos (FILE * stream, fops_t * pos);
int fsetpos (FILE * stream, fops_t * pos);
fseek function is to set the current position to offset Department, whence parameter determines the position relative to the document, there are three: SEEK_SET, SEEK_CUR, SEEK_END
 
6> format I / O
# include
int printf (const char * format ,...);
int fprintf (FILE * stream, const char * format ,...);
int sprintf (const char * format ,...);
int scanf (const char * format ,...);
int fscanf (FILE * stream, const char * format ,...);
For example:
fscantf (fp, "% s \ n", pp-> name);
fprintf (fp, buffer, "% f \ n");
 
7> Other functions
void clearerr (FILE * stream); / / clear the file stream error flag
FILE * fdopen (int fildes, const char * mode); / / say the document is converted to the corresponding description of the file pointer
int fflush (FILE * stream); / / will buffer the data stream into the specified parameter file
int fseek (FILE * stream, long offset, int whence); / / move files to read and write the location of flow
long ftell (FILE * stream); / / access to documents to read and write the location of the current flow
阅读(931) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~