介绍 C++ 头文件 — <cstdio>
头文件
<cstdio>
头文件
简介
<cstdio>
是将 <>
的内容用 C++ 头文件的形式表示出来。<>
是 C 标准函数库中的头文件,即:standard buffered input&output。提供基本的文字的输入输出流操作(包括屏幕和文件等)。由于 C 语言并没有提供专用于文字输入输出的关键字,所以该库是最普遍的 C 语言程序加载库。
注:<cstdio>
和 <>
是有差别的,并不是同样的文件。
概要
namespace std {
using size_t = ;
using FILE = ;
using fpos_t = ;
}
#define NULL
#define _IOFBF
#define _IOLBF
#define _IONBF
#define BUFSIZ
#define EOF
#define FOPEN_MAX
#define FILENAME_MAX
#define L_tmpnam
#define SEEK_CUR
#define SEEK_END
#define SEEK_SET
#define TMP_MAX
#define stderr
#define stdin
#define stdout
namespace std {
int remove(const char* filename);
int rename(const char* old, const char* new);
FILE* tmpfile();
char* tmpnam(char* s);
int fclose(FILE* stream);
int fflush(FILE* stream);
FILE* fopen(const char* filename, const char* mode);
FILE* freopen(const char* filename, const char* mode, FILE* stream);
void setbuf(FILE* stream, char* buf);
int setvbuf(FILE* stream, char* buf, int mode, size_t size);
int fprintf(FILE* stream, const char* format, ...);
int fscanf(FILE* stream, const char* format, ...);
int printf(const char* format, ...);
int scanf(const char* format, ...);
int snprintf(char* s, size_t n, const char* format, ...);
int sprintf(char* s, const char* format, ...);
int sscanf(const char* s, const char* format, ...);
int vfprintf(FILE* stream, const char* format, va_list arg);
int vfscanf(FILE* stream, const char* format, va_list arg);
int vprintf(const char* format, va_list arg);
int vscanf(const char* format, va_list arg);
int vsnprintf(char* s, size_t n, const char* format, va_list arg);
int vsprintf(char* s, const char* format, va_list arg);
int vsscanf(const char* s, const char* format, va_list arg);
int fgetc(FILE* stream);
char* fgets(char* s, int n, FILE* stream);
int fputc(int c, FILE* stream);
int fputs(const char* s, FILE* stream);
int getc(FILE* stream);
int getchar();
int putc(int c, FILE* stream);
int putchar(int c);
int puts(const char* s);
int ungetc(int c, FILE* stream);
size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);
int fgetpos(FILE* stream, fpos_t* pos);
int fseek(FILE* stream, long int offset, int whence);
int fsetpos(FILE* stream, const fpos_t* pos);
long int ftell(FILE* stream);
void rewind(FILE* stream);
void clearerr(FILE* stream);
int feof(FILE* stream);
int ferror(FILE* stream);
void perror(const char* s);
}
一些知识
执行输入/输出操作的 C++ 库
输入与输出操作在 C++ 中也可以通过使用 C 标准输入输出库(<cstdio>
,在 C 语言中为 <>
)实现。该库使用所谓的“流“操作物理设备,如键盘、打印机、终端或系统支持的其它类型文件。流是与这些物理设备以统一方式交互的抽象概念。所有的流都具有独立于它们相关联的物理设备个体特性的相似属性。
流在 <cstdio>
库中以指向 FILE 对象的指向方式操作。一个指向FILE对象的指针唯一表示一个流,并且在设计该流的操作中作为参数使用。
存在三种标准流:stdin、stdout 与 stderr,它们自动为所有使用该库的程序公开创建。
流属性
流拥有一些属性,定义了哪些函数可以作用于它们与如何对待从它们输入或输出的数据。这些属性的绝大多数在通过 fopen
函数将该流与文件关联(opened)的时刻就已经定义了:
读/写访问
指定该流是否具有它们关联的物理媒体的读或写访问权限(或两者都有)。
文本/二进制
文本流被认为是表示一些列文本行,每一行以换行符结束。根据程序运行环境的不同,为了使一些特殊字符适应文本文件环境的特殊性,一些字符转换也许会发生。另一方面,二进制流是不经过任何转换写入物理媒介或从物理媒介读出的一系列字符,它们与从流中读出或写入流的字符一一对应。
缓存
混村是一个内存块,数据在从关联的文件或设备中读入或写入之前累计的地方。流可以是全缓存、行缓存或不缓存。对于全缓存流,数据当该流满了时读取/写入,对于行缓存流,发生在遇到换行符时,对于不缓存流字符立即被读取/写入。
定向
在打开时,流是没有定向的。当输入/输出操作作用在它们身上时,它们变成面向字节的或面向宽字节的,这取决于作用的操作符(通常定义在中的函数是面向字节的,然而定义在中的函数是面向宽字节的)。
指示器
流用于特定的内部指示器,它们表名它们的当前状态与哪些影响作用于它们身上的输入输出操作的行为:
错误指示符
该指示符当错误发生在相对于流的操作时设置。该指示器可以通过 ferror
函数检测,并且可以通过调用 clearerr
、freopen
或 rewind
重设。
文件结束指示符
一旦设定,表明作用于该流上的下一个读或写操作到达文件结尾。它可以通过 feof
函数检测,并且可以通过调用 clearerr
、freopen
或调用任何重定位函数(rewind
,fseek
与 fsetpos
)重设。
定位指示符
它是每个流的一个内部指针,它指向读或写向下一个 I/O 操作的下一个字符。它的值可以通过 ftell
或 fgetpos
函数获取,并且可以通过使用重定位函数 rewind
、fseek
与 fsetpos
函数改变。
函数
操作文件:
-
remove
删除文件(函数) -
rename
重命名文件(函数) -
tmpfile
打开一个临时文件(函数) -
tmpnam
产生一个临时文件名(函数)
文件访问:
-
fclose
关闭文件(函数) -
fflush
冲刷流(函数) -
fopen
打开文件(函数) -
freopen
用不同的文件或模式重新打开流(函数) -
setbuf
设置流缓存(函数) -
servbuf
改变流缓存(函数)
格式化输入/输出:
fprintf
将格式化数据写入流(函数)
-
fscanf
从流中读格式化数据(函数) -
printf
打印格式化数据到stdout
(函数) -
scanf
从stdin
中读格式化数据(函数) -
snprintf
将格式化数据写入定长缓存(函数) -
sprintf
写格式化数据到字符串(函数) -
sscanf
从字符串中读格式化数据(函数) -
vfprintf
将格式化数据从变参列表写入流(函数) -
vfscanf
从流中读取格式化数据到变参列表(函数) -
vprintf
打印变参列表中的格式化数据到stdout
(函数) -
vscanf
读取格式化数据到变参列表(函数) -
vsnprintf
将变参列表中的格式化数据写入定长缓存(函数) -
vsprintf
将变参列表中的格式化数据写入字符串(函数) -
vsscanf
从字符串读取格式化数据到变参列表(函数)
字符串输入/输出:
-
fgetc
从流中获取字符(函数) -
fgets
从流中获取字符串(函数) -
fputc
将字符写入流(函数) -
fputs
将字符串写入流(函数) -
getc
从流中获取字符(函数) -
getchar
从stdin
中获取字符(函数) -
gets
从stdin
中获取字符串(函数) -
putc
将字符写入流(函数) -
putchar
将字符写入stdout
(函数) -
puts
将字符串写入stdout
(函数) -
ungetc
不从流中获取字符(函数)
直接输入/输出:
-
fread
从流中读取数据块(函数) -
fwrite
将数据块写入流(函数)
文件定位:
-
fgetpos
从流中获取当前位置(函数) -
fseek
重新定位流位置指示器(函数) -
fsetpos
设置流位置指示器(函数) -
ftell
获取流当前位置(函数) -
rewind
设置流位置为开始处(函数)
错误处理:
-
clearerr
清除错误指示器(函数) -
feof
检查文件结束指示器(函数) -
ferror
检查错误指示器(函数) -
perror
打印错误信息(函数)
宏
-
BUFSIZE
缓冲大小(常量) -
EOF
文件结束(常量) -
FILENAME_MAX
文件名的最大长度(常量) -
FOPEN_MAX
连续打开流的潜在限制(常量) -
L_tmpnam
临时文件名的最小长度(常量) -
NULLnull
指针(宏) -
TMP_MAX
临时文件数量(常量) -
此外:
_IOFBF
、_IOLBF
、_IONBF
(用在setvbuf
) -
SEEK_CUR
、SEEK_END
与SEEK_SET
(用在fseek
)。
类型
-
FILE
包含控制流的信息的对象(类型) -
fpos_t
包含在文件中指定位置的信息的对象(类型) -
size_t
无符号整数类型(类型)
内容
// -*- C++ -*- forwarding
header.
// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
// Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
//
// ISO C++ 14882: 27.8.2 C Library files
//
/** @file cstdio
* This is a Standard C++ Library file. You should @c #include this file
* in your programs, rather than any of the "*.h" implementation files.
*
* This is the C++ version of the Standard C Library header @,
* and its contents are (mostly) the same as that header, but are all
* contained in the namespace @cstd.
*/
#ifndef _GLIBCXX_CSTDIO
#define _GLIBCXX_CSTDIO 1
#pragma GCC system_header
#include <bits/c++>
#include <cstddef>
#include <>
// Get rid of those macros defined in <> in lieu of real functions.
#undef clearerr
#undef fclose
#undef feof
#undef ferror
#undef fflush
#undef fgetc
#undef fgetpos
#undef fgets
#undef fopen
#undef fprintf
#undef fputc
#undef fputs
#undef fread
#undef freopen
#undef fscanf
#undef fseek
#undef fsetpos
#undef ftell
#undef fwrite
#undef getc
#undef getchar
#undef gets
#undef perror
#undef printf
#undef putc
#undef putchar
#undef puts
#undef remove
#undef rename
#undef rewind
#undef scanf
#undef setbuf
#undef setvbuf
#undef sprintf
#undef sscanf
#undef tmpfile
#undef tmpnam
#undef ungetc
#undef vfprintf
#undef vprintf
#undef vsprintf
namespace std
{
using ::FILE;
using ::fpos_t;
using ::clearerr;
using ::fclose;
using ::feof;
using ::ferror;
using ::fflush;
using ::fgetc;
using ::fgetpos;
using ::fgets;
using ::fopen;
using ::fprintf;
using ::fputc;
using ::fputs;
using ::fread;
using ::freopen;
using ::fscanf;
using ::fseek;
using ::fsetpos;
using ::ftell;
using ::fwrite;
using ::getc;
using ::getchar;
using ::gets;
using ::perror;
using ::printf;
using ::putc;
using ::putchar;
using ::puts;
using ::remove;
using ::rename;
using ::rewind;
using ::scanf;
using ::setbuf;
using ::setvbuf;
using ::sprintf;
using ::sscanf;
using ::tmpfile;
using ::tmpnam;
using ::ungetc;
using ::vfprintf;
using ::vprintf;
using ::vsprintf;
}
#if _GLIBCXX_USE_C99
#undef snprintf
#undef vfscanf
#undef vscanf
#undef vsnprintf
#undef vsscanf
namespace __gnu_cxx
{
#if _GLIBCXX_USE_C99_CHECK || _GLIBCXX_USE_C99_DYNAMIC
extern "C" int
(snprintf)(char *
restrict,
size_t,
const char * restrict, ...);
extern "C" int
(
vfscanf)(FILE * restrict, const char * restrict, __gnuc_va_list);
extern "C" int (
vscanf)(const char *
restrict, __gnuc_va_list);
extern "C" int
(vsnprintf)(char * restrict,
size_t,
const char * restrict, __gnuc_va_list);
extern "C" int
(
vsscanf)(const char * restrict, const char * restrict, __gnuc_va_list);
#endif
#if !_GLIBCXX_USE_C99_DYNAMIC
using ::snprintf;
using ::
vfscanf;
using ::
vscanf;
using ::vsnprintf;
using ::vsscanf;
#endif
}
namespace std
{
using __gnu_cxx::
snprintf;
using __gnu_cxx::vfscanf;
using __gnu_cxx::vscanf;
using __gnu_cxx::vsnprintf;
using __gnu_cxx::
vsscanf;
}
#endif