<stdio.h>
...
typedef struct {
char *a0; /* pointer to first homed integer argument */
int offset; /* byte offset of next parameter */
} va_list;
#define BUFSIZ 4096
/*
* Default number of supported streams. _NFILE is confusing and obsolete, but
* supported anyway for backwards compatibility.
*/
#define _NFILE _NSTREAM_
#define _NSTREAM_ 512
/*
* Number of entries in _iob[] (declared below). Note that _NSTREAM_ must be
* greater than or equal to _IOB_ENTRIES.
*/
#define _IOB_ENTRIES 20
#define _NSTREAM_ 128
#define EOF (-1)
//文件结构定义
#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 L_tmpname 255 //临时名称的长度
/* Seek method constants */
#define SEEK_CUR 1
#define SEEK_END 2
#define SEEK_SET 0
#define FILENAME_MAX 260 //可以同时打开文件的最大数目
#define FOPEN_MAX 20
#define _SYS_OPEN 20
#define TMP_MAX 32767
。。。
#define stdin (&_iob[0])
#define stdout (&_iob[1])
#define stderr (&_iob[2])
#define _IOREAD 0x0001
#define _IOWRT 0x0002
#define _IOFBF 0x0000
#define _IOLBF 0x0040
#define _IONBF 0x0004
#define _IOMYBUF 0x0008
#define _IOEOF 0x0010
#define _IOERR 0x0020
#define _IOSTRG 0x0040
#define _IORW 0x0080
/* Function prototypes */
...
<--------------------------------------------------------------------------------------------
FILE* fopen(const char* filename, const char* mode);
打开文件filename 返回文件流(失败NULL)。mode: ("r":reading;"w": writing; "a" :append
"r+" :reading and writing;
"w+":text update, discarding previous content (if any)
"a+":text append, reading, and writing at end)
or one of those strings with b included (after the first character), for binary files.
FILE* freopen(const char* filename, const char* mode, FILE* stream);
Closes file associated with stream, then opens file filename with specified mode and associates it with stream. Returns
stream or NULL on error.
int fflush(FILE* stream);
Flushes stream and returns zero on success or EOF on error. Effect undefined for input stream. fflush(NULL)
flushes all output streams.
int fclose(FILE* stream);
Closes stream stream (after flushing, if output stream). Returns EOF on error, zero otherwise.
int remove(const char* filename);
Removes specified file. Returns non-zero on failure.
int rename(const char* oldname, const char* newname);//更改文件名。出错返回非0。
FILE* tmpfile();
Creates temporary file (mode "wb+") which will be removed when closed or on normal program termination.
Returns stream or NULL on failure.
char* tmpnam(char s[]);
Assigns to s (if s non-null) and returns unique name for a temporary file. Unique name is returned for each of the first
TMP_MAX invocations.
例:
int main ()
{
char buffer [L_tmpnam];
char * pointer;
tmpnam (buffer);
printf ("Tempname #1: %s/n",buffer);
pointer = tmpnam (NULL);
printf ("Tempname #2: %s/n",pointer);
return 0;
}
//缓冲区操作
int setvbuf(FILE* stream, char* buf, int mode, size_t size);
用指定的流填充IO操作中的缓冲:Changes the buffer to be used for I/O operations with the specified stream.
函数允许指定缓冲区的mode和size.
调用场合:文件和流关联、打开后,但是没有发生任何输入输出操作.
The size of the buffer is specified by the size parameter in bytes.
如果没有指定缓冲(buffer参数is NULL), 系统自动建立缓冲并且分配所需要的内存(大小:size)for the stream.
mode参数指定:1、fully buffered;2、line buffered; 3、unbuffered.
fully buffered: 写操作不直接写入相关的设备而是缓冲区装满后整块写入; fflush、fclose或者程序结束时块未满也将写入。
line buffered:缓冲区每写一行就往相关设备送.
unbuffered streams:无缓冲,写操作直接作用于设备。
All files are opened with a default allocated buffer. This function can be used to either redefine the buffer size, define a user-allocated buffer or to disable buffering for the file.
System standard streams like stdout and stderr are unbuffered by default if they are not redirected.
Parameters
stream:Pointer to a FILE object that identifies an open stream.
buffer:User allocated buffer. Must be at least size bytes long.
mode:_IOFBF、_IOLBF、_IONBF
size:Buffer size in bytes.
返回:成功,0;失败:非0;
示例:
int main ()
{
char buffer[BUFSIZ];
FILE *pFile1, *pFile2;
pFile1=fopen ("myfile.txt","w");
pFile2=fopen ("myfile2.txt","a");
setbuf ( pFile1 , buffer );
fputs ("This is sent to a buffered stream",pFile1);
fflush (pFile1);
setbuff ( pFile2 , NULL );
fputs ("This is sent to an unbuffered stream",pFile2);
fclose (pFile1);
fclose (pFile2);
return 0;
}
void setbuf ( FILE * stream, char * buffer );
。。。
//输入输出
int fprintf(FILE* stream, const char* format, ...);
int printf(const char* format, ...);
printf(f, ...) is equivalent to fprintf(stdout, f, ...)
int sprintf(char* s, const char* format, ...);
Like fprintf, but output written into string s, which must be large enough to hold the output, rather than to a stream.
Output is NUL-terminated. Returns length (excluding the terminating NUL).
int vfprintf(FILE* stream, const char* format, va_list arg);
Equivalent to fprintf with variable argument list replaced by arg, which must have been initialised by the va_start macro
(and may have been used in calls to va_arg).
int vprintf(const char* format, va_list arg);
Equivalent to printf with variable argument list replaced by arg, which must have been initialised by the va_start macro
(and may have been used in calls to va_arg).
int vsprintf(char* s, const char* format, va_list arg);
Equivalent to sprintf with variable argument list replaced by arg, which must have been initialised by the va_start macro
(and may have been used in calls to va_arg).
int fscanf(FILE* stream, const char* format, ...);
int scanf(const char* format, ...);
scanf(f, ...) is equivalent to fscanf(stdin, f, ...)
int sscanf(char* s, const char* format, ...);
Like fscanf, but input read from string s.
//
int fgetc(FILE* stream);
Returns next character from (input) stream stream, or EOF on end-of-file or error.
char* fgets(char* s, int n, FILE* stream);
Copies characters from (input) stream stream to s, stopping when n-1 characters copied, newline copied, end-of-file
reached or error occurs. If no error, s is NUL-terminated. Returns NULL on end-of-file or error, s otherwise.
int fputc(int c, FILE* stream);
Writes c, to stream stream. Returns c, or EOF on error.
char* fputs(const char* s, FILE* stream);
Writes s, to (output) stream stream. Returns non-negative on success or EOF on error.
int getc(FILE* stream);
Equivalent to fgetc except that it may be a macro.
int getchar(void);
Equivalent to getc(stdin).
char* gets(char* s);
Copies characters from stdin into s until newline encountered, end-of-file reached, or error occurs. Does not copy
newline. NUL-terminates s. Returns s, or NULL on end-of-file or error. Should not be used because of the potential for buffer
overflow.
int putc(int c, FILE* stream);
Equivalent to fputc except that it may be a macro.
int putchar(int c);
putchar(c) is equivalent to putc(c, stdout).
int puts(const char* s);
Writes s (excluding terminating NUL) and a newline to stdout. Returns non-negative on success, EOF on error.
int ungetc(int c, FILE* stream);
Pushes c (which must not be EOF), onto (input) stream stream such that it will be returned by the next read. Only one
character of pushback is guaranteed (for each stream). Returns c, or EOF on error.
//文件流操作
size_t fread(void* ptr, size_t size, size_t nobj, FILE* stream);
读函数。
Reads (at most) nobj objects of size size from stream stream into ptr and returns number of objects read. (feof and ferror
can be used to check status.)
size_t fwrite(const void* ptr, size_t size, size_t nobj, FILE* stream);
写函数。
源:* ptr, 取长度:size; 写入目标:FILE* stream,长度:nobj。
返回:写入结果的长度。
int fseek(FILE* stream, long offset, int origin);
//在流中设置指针位置并且清除end-of-file指示。对于1个2进制的文件流,指针被这样的放置:偏移量(offset)、相对于原点位置(有3种
:1、起始位置SEEK_SET;2、当前位置SEEK_CUR;3、结束位置SEEK_END)。出错返回非0.
long ftell(FILE* stream); //Returns current file position for stream stream, or -1 on error.
void rewind(FILE* stream); //Equivalent to fseek(stream, 0L, SEEK_SET); clearerr(stream).
int fgetpos(FILE* stream, fpos_t* ptr); //得到流的当前指针. 出错返回非0.
int fsetpos(FILE* stream, const fpos_t* ptr); //设置流的当前指针to *ptr. 出错返回非0.
void clearerr(FILE* stream); //清除end-of-file 和 error指示
int feof(FILE* stream); //返回非0值 if end-of-file indicator is set for stream stream.
int ferror(FILE* stream); //返回非0值(if error indicator is set for stream stream).
void perror(const char* s);
Prints s (if non-null) and strerror(errno) to standard error as would:
fprintf(stderr, "%s: %s/n", (s != NULL ? s : ""), strerror(errno))
...
typedef struct {
char *a0; /* pointer to first homed integer argument */
int offset; /* byte offset of next parameter */
} va_list;
#define BUFSIZ 4096
/*
* Default number of supported streams. _NFILE is confusing and obsolete, but
* supported anyway for backwards compatibility.
*/
#define _NFILE _NSTREAM_
#define _NSTREAM_ 512
/*
* Number of entries in _iob[] (declared below). Note that _NSTREAM_ must be
* greater than or equal to _IOB_ENTRIES.
*/
#define _IOB_ENTRIES 20
#define _NSTREAM_ 128
#define EOF (-1)
//文件结构定义
#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 L_tmpname 255 //临时名称的长度
/* Seek method constants */
#define SEEK_CUR 1
#define SEEK_END 2
#define SEEK_SET 0
#define FILENAME_MAX 260 //可以同时打开文件的最大数目
#define FOPEN_MAX 20
#define _SYS_OPEN 20
#define TMP_MAX 32767
。。。
#define stdin (&_iob[0])
#define stdout (&_iob[1])
#define stderr (&_iob[2])
#define _IOREAD 0x0001
#define _IOWRT 0x0002
#define _IOFBF 0x0000
#define _IOLBF 0x0040
#define _IONBF 0x0004
#define _IOMYBUF 0x0008
#define _IOEOF 0x0010
#define _IOERR 0x0020
#define _IOSTRG 0x0040
#define _IORW 0x0080
/* Function prototypes */
...
<--------------------------------------------------------------------------------------------
FILE* fopen(const char* filename, const char* mode);
打开文件filename 返回文件流(失败NULL)。mode: ("r":reading;"w": writing; "a" :append
"r+" :reading and writing;
"w+":text update, discarding previous content (if any)
"a+":text append, reading, and writing at end)
or one of those strings with b included (after the first character), for binary files.
FILE* freopen(const char* filename, const char* mode, FILE* stream);
Closes file associated with stream, then opens file filename with specified mode and associates it with stream. Returns
stream or NULL on error.
int fflush(FILE* stream);
Flushes stream and returns zero on success or EOF on error. Effect undefined for input stream. fflush(NULL)
flushes all output streams.
int fclose(FILE* stream);
Closes stream stream (after flushing, if output stream). Returns EOF on error, zero otherwise.
int remove(const char* filename);
Removes specified file. Returns non-zero on failure.
int rename(const char* oldname, const char* newname);//更改文件名。出错返回非0。
FILE* tmpfile();
Creates temporary file (mode "wb+") which will be removed when closed or on normal program termination.
Returns stream or NULL on failure.
char* tmpnam(char s[]);
Assigns to s (if s non-null) and returns unique name for a temporary file. Unique name is returned for each of the first
TMP_MAX invocations.
例:
int main ()
{
char buffer [L_tmpnam];
char * pointer;
tmpnam (buffer);
printf ("Tempname #1: %s/n",buffer);
pointer = tmpnam (NULL);
printf ("Tempname #2: %s/n",pointer);
return 0;
}
//缓冲区操作
int setvbuf(FILE* stream, char* buf, int mode, size_t size);
用指定的流填充IO操作中的缓冲:Changes the buffer to be used for I/O operations with the specified stream.
函数允许指定缓冲区的mode和size.
调用场合:文件和流关联、打开后,但是没有发生任何输入输出操作.
The size of the buffer is specified by the size parameter in bytes.
如果没有指定缓冲(buffer参数is NULL), 系统自动建立缓冲并且分配所需要的内存(大小:size)for the stream.
mode参数指定:1、fully buffered;2、line buffered; 3、unbuffered.
fully buffered: 写操作不直接写入相关的设备而是缓冲区装满后整块写入; fflush、fclose或者程序结束时块未满也将写入。
line buffered:缓冲区每写一行就往相关设备送.
unbuffered streams:无缓冲,写操作直接作用于设备。
All files are opened with a default allocated buffer. This function can be used to either redefine the buffer size, define a user-allocated buffer or to disable buffering for the file.
System standard streams like stdout and stderr are unbuffered by default if they are not redirected.
Parameters
stream:Pointer to a FILE object that identifies an open stream.
buffer:User allocated buffer. Must be at least size bytes long.
mode:_IOFBF、_IOLBF、_IONBF
size:Buffer size in bytes.
返回:成功,0;失败:非0;
示例:
int main ()
{
char buffer[BUFSIZ];
FILE *pFile1, *pFile2;
pFile1=fopen ("myfile.txt","w");
pFile2=fopen ("myfile2.txt","a");
setbuf ( pFile1 , buffer );
fputs ("This is sent to a buffered stream",pFile1);
fflush (pFile1);
setbuff ( pFile2 , NULL );
fputs ("This is sent to an unbuffered stream",pFile2);
fclose (pFile1);
fclose (pFile2);
return 0;
}
void setbuf ( FILE * stream, char * buffer );
。。。
//输入输出
int fprintf(FILE* stream, const char* format, ...);
int printf(const char* format, ...);
printf(f, ...) is equivalent to fprintf(stdout, f, ...)
int sprintf(char* s, const char* format, ...);
Like fprintf, but output written into string s, which must be large enough to hold the output, rather than to a stream.
Output is NUL-terminated. Returns length (excluding the terminating NUL).
int vfprintf(FILE* stream, const char* format, va_list arg);
Equivalent to fprintf with variable argument list replaced by arg, which must have been initialised by the va_start macro
(and may have been used in calls to va_arg).
int vprintf(const char* format, va_list arg);
Equivalent to printf with variable argument list replaced by arg, which must have been initialised by the va_start macro
(and may have been used in calls to va_arg).
int vsprintf(char* s, const char* format, va_list arg);
Equivalent to sprintf with variable argument list replaced by arg, which must have been initialised by the va_start macro
(and may have been used in calls to va_arg).
int fscanf(FILE* stream, const char* format, ...);
int scanf(const char* format, ...);
scanf(f, ...) is equivalent to fscanf(stdin, f, ...)
int sscanf(char* s, const char* format, ...);
Like fscanf, but input read from string s.
//
int fgetc(FILE* stream);
Returns next character from (input) stream stream, or EOF on end-of-file or error.
char* fgets(char* s, int n, FILE* stream);
Copies characters from (input) stream stream to s, stopping when n-1 characters copied, newline copied, end-of-file
reached or error occurs. If no error, s is NUL-terminated. Returns NULL on end-of-file or error, s otherwise.
int fputc(int c, FILE* stream);
Writes c, to stream stream. Returns c, or EOF on error.
char* fputs(const char* s, FILE* stream);
Writes s, to (output) stream stream. Returns non-negative on success or EOF on error.
int getc(FILE* stream);
Equivalent to fgetc except that it may be a macro.
int getchar(void);
Equivalent to getc(stdin).
char* gets(char* s);
Copies characters from stdin into s until newline encountered, end-of-file reached, or error occurs. Does not copy
newline. NUL-terminates s. Returns s, or NULL on end-of-file or error. Should not be used because of the potential for buffer
overflow.
int putc(int c, FILE* stream);
Equivalent to fputc except that it may be a macro.
int putchar(int c);
putchar(c) is equivalent to putc(c, stdout).
int puts(const char* s);
Writes s (excluding terminating NUL) and a newline to stdout. Returns non-negative on success, EOF on error.
int ungetc(int c, FILE* stream);
Pushes c (which must not be EOF), onto (input) stream stream such that it will be returned by the next read. Only one
character of pushback is guaranteed (for each stream). Returns c, or EOF on error.
//文件流操作
size_t fread(void* ptr, size_t size, size_t nobj, FILE* stream);
读函数。
Reads (at most) nobj objects of size size from stream stream into ptr and returns number of objects read. (feof and ferror
can be used to check status.)
size_t fwrite(const void* ptr, size_t size, size_t nobj, FILE* stream);
写函数。
源:* ptr, 取长度:size; 写入目标:FILE* stream,长度:nobj。
返回:写入结果的长度。
int fseek(FILE* stream, long offset, int origin);
//在流中设置指针位置并且清除end-of-file指示。对于1个2进制的文件流,指针被这样的放置:偏移量(offset)、相对于原点位置(有3种
:1、起始位置SEEK_SET;2、当前位置SEEK_CUR;3、结束位置SEEK_END)。出错返回非0.
long ftell(FILE* stream); //Returns current file position for stream stream, or -1 on error.
void rewind(FILE* stream); //Equivalent to fseek(stream, 0L, SEEK_SET); clearerr(stream).
int fgetpos(FILE* stream, fpos_t* ptr); //得到流的当前指针. 出错返回非0.
int fsetpos(FILE* stream, const fpos_t* ptr); //设置流的当前指针to *ptr. 出错返回非0.
void clearerr(FILE* stream); //清除end-of-file 和 error指示
int feof(FILE* stream); //返回非0值 if end-of-file indicator is set for stream stream.
int ferror(FILE* stream); //返回非0值(if error indicator is set for stream stream).
void perror(const char* s);
Prints s (if non-null) and strerror(errno) to standard error as would:
fprintf(stderr, "%s: %s/n", (s != NULL ? s : ""), strerror(errno))