标准I/O-2-文件结束或出错,EOF,fgetc,feof,ferror,clearerr,rewind(未完)

 

参考博文:

http://bbs.chinaunix.net/thread-233220-1-1.html

http://www.cnitblog.com/guopingleee/archive/2009/01/29/54047.html

http://hi.baidu.com/qq630727668%BA%CD51wangying19881118/blog/item/6bc6493395c80f4bad4b5fa3.html

http://www.cnblogs.com/xbspring/archive/2009/01/08/1371696.html(文本文件与二进制文件

http://www.cnblogs.com/tenghoo/archive/2008/06/01/1211663.html(原码、补码和反码

 

----------FILE定义
1、网上流传的版本

/* <stdio.h> */
typedef struct {
	int level; /* fill/empty level of buffer */
	unsigned flags; /* File status flags */
	char fd; /* File descriptor */
	unsigned char hold; /* Ungetc char if no buffer */
	int bsize; /* Buffer size */
	unsigned char _FAR *buffer; /* Data transfer buffer */
	unsigned char _FAR *curp; /* Current active pointer */
	unsigned istemp; /* Temporary file indicator */
	short token; /* Used for validity checking */
} FILE; /* This is the FILE object */

2、VS2008

/* <stdio.h> */
struct _iobuf {
	char *_ptr;//文件内部指针
	int   _cnt;
	char *_base;
	int   _flag;
	int   _file;
	int   _charbuf;
	int   _bufsiz;
	char *_tmpfname;
};
typedef struct _iobuf FILE;

3、Linux GCC

/* <libio.h> */
struct _IO_FILE {
  int _flags;		/* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  char* _IO_read_ptr;	/* Current read pointer */
  char* _IO_read_end;	/* End of get area. */
  char* _IO_read_base;	/* Start of putback+get area. */
  char* _IO_write_base;	/* Start of put area. */
  char* _IO_write_ptr;	/* Current put pointer. */
  char* _IO_write_end;	/* End of put area. */
  char* _IO_buf_base;	/* Start of reserve area. */
  char* _IO_buf_end;	/* End of reserve area. */
  /* The following fields are used to support backing up and undo. */
  char *_IO_save_base; /* Pointer to start of non-current get area. */
  char *_IO_backup_base;  /* Pointer to first valid character of backup area */
  char *_IO_save_end; /* Pointer to end of non-current get area. */

  struct _IO_marker *_markers;

  struct _IO_FILE *_chain;

  int _fileno;
  int _blksize;
  _IO_off_t _offset;

#define __HAVE_COLUMN /* temporary */
  /* 1+column number of pbase(); 0 is unknown. */
  unsigned short _cur_column;
  char _unused;
  char _shortbuf[1];

  /*  char* _save_gptr;  char* _save_egptr; */

  _IO_lock_t *_lock;
};

/* <stdio.h> */
#include <libio.h>
typedef struct _IO_FILE FILE;


----------函数原型及作用

int feof(FILE *stream);
The function feof() tests the end-of-file indicator for the stream pointed to by stream, returning non-zero if it is set.The end-of-file indicator can only be cleared by the function clearerr().
说明:测试文件内部指针是否到达文件末尾

int ferror(FILE *stream);
The function ferror() tests the error indicator for the stream pointed to by stream, returning non-zero  if  it  is  set.The error indicator can only be reset by the clearerr() function.
说明:测试文件内部指针是否出错

int fgetc(FILE *stream);
reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.
MSDN中的一段话:use feof or ferror to distinguish between an error and an end-of-file condition(用feof和ferror来区分end-of-file和error两种情况)
说明:返回值有两类,一类是获取到的字符(被转化为int型了),二类是返回-1(EOF)(见下面的【重点讨论】)

void clearerr(FILE *stream);
The function clearerr() clears the end-of-file and error indicators for the stream pointed to by stream.
说明:清除end-of-file indicator和error indicator。【重点】FILE结构体里有记录文件内部指针到达文件末尾和出错的标志变量,当用fgetc()处理文件时,如果到达文件末尾或出错,相应的标志变量会置于出错状态,clearerr()的作用是将这两个标志变量置于正常状态

void rewind(FILE *stream);
(下面这段摘自MSDN)
The rewind function repositions the file pointer associated with stream to the beginning of the file. A call to rewind is similar to
(void) fseek( stream, 0L, SEEK_SET );
However, unlike fseek, rewind clears the error indicators for the stream as well as the end-of-file indicator. Also, unlike fseek, rewind does not return a value to
indicate whether the pointer was successfully moved.
说明:rewind()的作用不但是将文件内部指针回置开始处,而且还会清除end-of-file indicator和error indicator


----------如何判断一个文件内部指针已到达文件末尾?——EOF和feof()
****以下都以fgetc()读取为例
1、feof()判断
对文本文件和二进制文件均使用,但须注意一点,如下:
----例1(错误)(谭浩强的书中是这样的)

char c;
while(!feof(fp))
{
	c = fgetc(fp);
	printf("%X\n", c); 
}
当fgetc()读到文件末尾时,还会输出一次,即0xFF(-1)

----例2(正确)

char c;
c = fgetc(fp);
while(!feof(fp))
{
	printf("%X\n", c); 
	c = fgetc(fp);
}

2、EOF判断

(未完待续)


----------补充——原码、反码、补码
对于char型数据:char ch = oxFF;
ch的二进制形式是多少?-1
(FF)H --> (1111,1111)B——这是补码形式 --> (1000,0001)B——这是原码形式,即-1

对于int型数据:int a = 0xFF;
a的二进制形式是多少?255
注:gcc中int型占4个byte
(FF)H --> (00000000,00000000,00000000,11111111)——由于最高位为0,即正数,正数的原码和补码相同

那int a = -1;的十六进制是多少?0xFFFFFFFF

你可能感兴趣的:(Stream,IO,struct,function,File,returning)