C++ stdio.h详解

一般地,在C语言或C++中,会把用来#include的文件的扩展名叫 .h,称其为头文件。
#include文件的目的就是把多个编译单元(也就是c或者cpp文件)公用的内容,单独放在一个文件里减少整体代码尺寸;或者提供跨工程公共代码。在现行的c++版本中,应用这个头文件应是#include。

目录
1 简介
2 引用方法
3 标准函数
4 文件内容
▪ 文件说明
▪ 宏定义
▪ 结构体定义
▪ 操作函数定义

简介

stdio 就是指 “standard input & output"(标准输入输出)
所以,源代码中如用到标准输入输出函数时,就要包含这个头文件!
例如c语言中的 printf("%d",i); scanf("%d",&i);等函数。

引用方法

#include<stdio.h>

(注:在TC2.0中,允许不引用此头文件而直接调用其中的函数,但这种做法是不标准的。也不建议这样做。以避免出现在其他IDE中无法编译或执行的问题。)

标准函数

int getchar()

//从标准输入设备写入一个字符

int putchar()

//向标准输出设备读出一个字符

int scanf(char*format[,argument…])

//从标准输入设备读入格式化后的数据

int printf(char*format[,argument…])

//向标准输出设备输出格式化字符串

char* gets(char*string)

//从标准输入设备读入一个字符串

int puts(char*string)

//向标准输出设备输出一个字符串

int sprintf(charstring,charformat[,…])

//把格式化的数据写入某个字符串缓冲区

文件内容

文件说明

/*
*stdio.h
*This file has no copyright assigned and is placed in the Public Domain.
*This file is a part of the mingw-runtime package.
*Now arranty is given;refer to the file DISCLAIMER within the package.
*
*Definitions of types and proto types of functions for standard inputand output.
*
*NOTE:The file manipulation functions provided by Microsoft seem to
*work with either slash(/) or backslash(\) as the directory separator.
*
*/

宏定义

#ifndef_STDIO_H_
#define_STDIO_H_
/*Alltheheadersincludethisfile.*/
#include<_mingw.h>
#ifndefRC_INVOKED
#define__need_size_t
#define__need_NULL
#define__need_wchar_t
#define__need_wint_t
#include<stddef.h>
#define__need___va_list
#include<stdarg.h>
#endif/*NotRC_INVOKED*/
/*Flagsfortheiobufstructure*/
#define_IOREAD1/*currentlyreading*/
#define_IOWRT2/*currentlywriting*/
#define_IORW0x0080/*openedas"r+w"*/
/*
*Thethreestandardfilepointersprovidedbytheruntimelibrary.
*NOTE:Thesewillgotothebit-bucketsilentlyinGUIapplications!
*/
#defineSTDIN_FILENO0
#defineSTDOUT_FILENO1
#defineSTDERR_FILENO2
/*Returnedbyvariousfunctionsonendoffileconditionorerror.*/
#defineEOF(-1)
/*
*Themaximumlengthofafilename.YoushoulduseGetVolumeInformation
*insteadofthisconstant.Buthey,thisworks.
*Alsodefinedinio.h.
*/
#ifndefFILENAME_MAX
#defineFILENAME_MAX(260)
#endif
/*
*Themaximumnumberoffilesthatmaybeopenatonce.Ihavesetthisto
*aconservativenumber.Theactualvaluemaybehigher.
*/
#defineFOPEN_MAX(20)
/*Aftercreatingthismanynames,tmpnamandtmpfilereturnNULL*/
#defineTMP_MAX32767
/*
*Tmpnam,tmpfileand,sometimes,_tempnamtrytocreate
*tempfilesintherootdirectoryofthecurrentdrive
*(notinpwd,assuggestedbysomeolderMSdoc's).
*RedefiningthesemacrosdoesnoteffecttheCRTfunctions.
*/
#define_P_tmpdir"\\"
#ifndef__STRICT_ANSI__
#defineP_tmpdir_P_tmpdir
#endif
#define_wP_tmpdirL"\\"
/*
*Themaximumsizeofname(includingNUL)thatwillbeputintheuser
*suppliedbuffercaNamefortmpnam.
*Inferredfromthesizeofthestaticbufferreturnedbytmpnam
*whenpassedaNULLargument.Mayactuallybesmaller.
*/
#defineL_tmpnam(16)
#define_IOFBF0x0000/*fullbuffered*/
#define_IOLBF0x0040/*linebuffered*/
#define_IONBF0x0004/*notbuffered*/
#define_IOMYBUF0x0008/*stdiomalloc()'dbuffer*/
#define_IOEOF0x0010/*EOFreachedonread*/
#define_IOERR0x0020/*I/Oerrorfromsystem*/
#define_IOSTRG0x0040/*Strangeornofiledescriptor*/
#ifdef_POSIX_SOURCE
#define_IOAPPEND0x0200
#endif
/*
*Thebuffersizeasusedbysetbufsuchthatitisequivalentto
*(void)setvbuf(fileSetBuffer,caBuffer,_IOFBF,BUFSIZ).
*/
#defineBUFSIZ512
/*ConstantsfornOriginindicatingthepositionrelativetowhichfseek
*setsthefileposition.Enclosedinifdefsbecauseio.hcouldalso
*definethem.(Thoughnotanymoresinceio.hincludesthisfilenow.)*/
#ifndefSEEK_SET
#defineSEEK_SET(0)
#endif
#ifndefSEEK_CUR
#defineSEEK_CUR(1)
#endif
#ifndefSEEK_END
#defineSEEK_END(2)
#endif
#ifndefRC_INVOKED
#ifndef__VALIST
#ifdef__GNUC__
#define__VALIST__gnuc_va_list
#else
#define__VALISTchar*
#endif
#endif/*defined__VALIST*/
/*
*ThestructureunderlyingtheFILEtype.
*
*Somebelievethatnobodyintheirrightmindshouldmakeuseofthe
*internalsofthisstructure.ProvidedbyPedroA.ArandaGutiirrez
*/
#ifndef_FILE_DEFINED
#define_FILE_DEFINED

结构体定义

typedefstruct_iobuf
{
char*_ptr;
int_cnt;
char*_base;
int_flag;
int_file;
int_charbuf;
int_bufsiz;
char*_tmpfname;
}FILE;
#endif/*Not_FILE_DEFINED*/
/*
*Thestandardfilehandles
*/
#ifndef__DECLSPEC_SUPPORTED
externFILE(*_imp___iob)[];/*ApointertoanarrayofFILE*/
#define_iob(*_imp___iob)/*AnarrayofFILE*/
#else/*__DECLSPEC_SUPPORTED*/
__MINGW_IMPORTFILE_iob[];/*AnarrayofFILEimportedfromDLL.*/
#endif/*__DECLSPEC_SUPPORTED*/
#definestdin(&_iob[STDIN_FILENO])
#definestdout(&_iob[STDOUT_FILENO])
#definestderr(&_iob[STDERR_FILENO])

操作函数定义

#ifdef__cplusplus
extern"C"{
#endif
/*
*FileOperations
*/
_CRTIMPFILE*__cdeclfopen(constchar*,constchar*);
_CRTIMPFILE*__cdeclfreopen(constchar*,constchar*,FILE*);
_CRTIMPint__cdeclfflush(FILE*);
_CRTIMPint__cdeclfclose(FILE*);
/*MSputsremove&rename(butnotwideversions)inio.halso*/
_CRTIMPint__cdeclremove(constchar*);
_CRTIMPint__cdeclrename(constchar*,constchar*);
_CRTIMPFILE*__cdecltmpfile(void);
_CRTIMPchar*__cdecltmpnam(char*);
#ifndef__STRICT_ANSI__
_CRTIMPchar*__cdecl_tempnam(constchar*,constchar*);
_CRTIMPint__cdecl_rmtmp(void);
#ifndefNO_OLDNAMES
_CRTIMPchar*__cdecltempnam(constchar*,constchar*);
_CRTIMPint__cdeclrmtmp(void);
#endif
#endif/*__STRICT_ANSI__*/
_CRTIMPint__cdeclsetvbuf(FILE*,char*,int,size_t);
_CRTIMPvoid__cdeclsetbuf(FILE*,char*);
/*
*FormattedOutput
*/
_CRTIMPint__cdeclfprintf(FILE*,constchar*,...);
_CRTIMPint__cdeclprintf(constchar*,...);
_CRTIMPint__cdeclsprintf(char*,constchar*,...);
_CRTIMPint__cdecl_snprintf(char*,size_t,constchar*,...);
_CRTIMPint__cdeclvfprintf(FILE*,constchar*,__VALIST);
_CRTIMPint__cdeclvprintf(constchar*,__VALIST);
_CRTIMPint__cdeclvsprintf(char*,constchar*,__VALIST);
_CRTIMPint__cdecl_vsnprintf(char*,size_t,constchar*,__VALIST);
#ifndef__NO_ISOCEXT/*externsinlibmingwex.a*/
int__cdeclsnprintf(char*s,size_tn,constchar*format,...);
__CRT_INLINEint__cdecl
vsnprintf(char*s,size_tn,constchar*format,__VALISTarg)
{return_vsnprintf(s,n,format,arg);}
int__cdeclvscanf(constchar*__restrict__,__VALIST);
int__cdeclvfscanf(FILE*__restrict__,constchar*__restrict__,
__VALIST);
int__cdeclvsscanf(constchar*__restrict__,
constchar*__restrict__,__VALIST);
#endif
/*
*FormattedInput
*/
_CRTIMPint__cdeclfscanf(FILE*,constchar*,...);
_CRTIMPint__cdeclscanf(constchar*,...);
_CRTIMPint__cdeclsscanf(constchar*,constchar*,...);
/*
*CharacterInputandOutputFunctions
*/
_CRTIMPint__cdeclfgetc(FILE*);
_CRTIMPchar*__cdeclfgets(char*,int,FILE*);
_CRTIMPint__cdeclfputc(int,FILE*);
_CRTIMPint__cdeclfputs(constchar*,FILE*);
_CRTIMPchar*__cdeclgets(char*);
_CRTIMPint__cdeclputs(constchar*);
_CRTIMPint__cdeclungetc(int,FILE*);
/*Traditionally,getcandputcaredefinedasmacros.butthe
standarddoesn'tsaythattheymustbemacros.
Weuseinlinefunctionsheretoallowthefastversions
tobeusedinC++withnamespacequalification,eg.,::getc.
_filbufand_flsbufarenotthread-safe.*/
_CRTIMPint__cdecl_filbuf(FILE*);
_CRTIMPint__cdecl_flsbuf(int,FILE*);
#if!defined_MT
__CRT_INLINEint__cdeclgetc(FILE*__F)
{
return(--__F->_cnt>=0)
?(int)(unsignedchar)*__F->_ptr++
:_filbuf(__F);
}
__CRT_INLINEint__cdeclputc(int__c,FILE*__F)
{
return(--__F->_cnt>=0)
?(int)(unsignedchar)(*__F->_ptr++=(char)__c)
:_flsbuf(__c,__F);
}
__CRT_INLINEint__cdeclgetchar(void)
{
return(--stdin->_cnt>=0)
?(int)(unsignedchar)*stdin->_ptr++
:_filbuf(stdin);
}
__CRT_INLINEint__cdeclputchar(int__c)
{
return(--stdout->_cnt>=0)
?(int)(unsignedchar)(*stdout->_ptr++=(char)__c)
:_flsbuf(__c,stdout);}
#else/*Uselibraryfunctions.*/
_CRTIMPint__cdeclgetc(FILE*);
_CRTIMPint__cdeclputc(int,FILE*);
_CRTIMPint__cdeclgetchar(void);
_CRTIMPint__cdeclputchar(int);
#endif
/*
*DirectInputandOutputFunctions
*/
_CRTIMPsize_t__cdeclfread(void*,size_t,size_t,FILE*);
_CRTIMPsize_t__cdeclfwrite(constvoid*,size_t,size_t,FILE*);
/*
*FilePositioningFunctions
*/
_CRTIMPint__cdeclfseek(FILE*,long,int);
_CRTIMPlong__cdeclftell(FILE*);
_CRTIMPvoid__cdeclrewind(FILE*);
#ifdef__USE_MINGW_FSEEK/*Theseareinlibmingwex.a*/
/*
*Workaroundforlimitationsonwin9xwhereafilecontentsare
*notzero'doutifyouseekpasttheendandthenwrite.
*/
int__cdecl__mingw_fseek(FILE*,long,int);
int__cdecl__mingw_fwrite(constvoid*,size_t,size_t,FILE*);
#definefseek(fp,offset,whence)__mingw_fseek(fp,offset,whence)
#definefwrite(buffer,size,count,fp)__mingw_fwrite(buffer,size,count,fp)
#endif/*__USE_MINGW_FSEEK*/
/*
*Anopaquedatatypeusedforstoringfilepositions...Thecontentsof
*thistypeareunknown,butwe(thecompiler)needtoknowthesize
*becausetheprogrammerusingfgetposandfsetposwillbesettingaside
*storageforfpos_tstructres.ActuallyItestedusingabytearrayand
*itisfairlyevidentthatthefpos_ttypeisalong(inCRTDLL.DLL).
*Perhapsanunsignedlong?TODO?It'sdefinitelya64-bitnumberin
*MSVCRThowever,andfornow`longlong'willdo.
*/
#ifdef__MSVCRT__
typedeflonglongfpos_t;
#else
typedeflongfpos_t;
#endif
_CRTIMPint__cdeclfgetpos(FILE*,fpos_t*);
_CRTIMPint__cdeclfsetpos(FILE*,constfpos_t*);
/*
*ErrorFunctions
*/
_CRTIMPint__cdeclfeof(FILE*);
_CRTIMPint__cdeclferror(FILE*);
#ifdef__cplusplus
inlineint__cdeclfeof(FILE*__F)
{return__F->_flag&_IOEOF;}
inlineint__cdeclferror(FILE*__F)
{return__F->_flag&_IOERR;}
#else
#definefeof(__F)((__F)->_flag&_IOEOF)
#defineferror(__F)((__F)->_flag&_IOERR)
#endif
_CRTIMPvoid__cdeclclearerr(FILE*);
_CRTIMPvoid__cdeclperror(constchar*);
#ifndef__STRICT_ANSI__
/*
*Pipes
*/
_CRTIMPFILE*__cdecl_popen(constchar*,constchar*);
_CRTIMPint__cdecl_pclose(FILE*);
#ifndefNO_OLDNAMES
_CRTIMPFILE*__cdeclpopen(constchar*,constchar*);
_CRTIMPint__cdeclpclose(FILE*);
#endif
/*
*OtherNonANSIfunctions
*/
_CRTIMPint__cdecl_flushall(void);
_CRTIMPint__cdecl_fgetchar(void);
_CRTIMPint__cdecl_fputchar(int);
_CRTIMPFILE*__cdecl_fdopen(int,constchar*);
_CRTIMPint__cdecl_fileno(FILE*);
_CRTIMPint__cdecl_fcloseall(void);
_CRTIMPFILE*__cdecl_fsopen(constchar*,constchar*,int);
#ifdef__MSVCRT__
_CRTIMPint__cdecl_getmaxstdio(void);
_CRTIMPint__cdecl_setmaxstdio(int);
#endif
#ifndef_NO_OLDNAMES
_CRTIMPint__cdeclfgetchar(void);
_CRTIMPint__cdeclfputchar(int);
_CRTIMPFILE*__cdeclfdopen(int,constchar*);
_CRTIMPint__cdeclfileno(FILE*);
#endif/*Not_NO_OLDNAMES*/
#define_fileno(__F)((__F)->_file)
#ifndef_NO_OLDNAMES
#definefileno(__F)((__F)->_file)
#endif
#ifdefined(__MSVCRT__)&&!defined(__NO_MINGW_LFS)
#include<sys/types.h>
__CRT_INLINEFILE*__cdeclfopen64(constchar*filename,constchar*mode)
{
returnfopen(filename,mode);
}
int__cdeclfseeko64(FILE*,off64_t,int);
#ifdef__USE_MINGW_FSEEK
int__cdecl__mingw_fseeko64(FILE*,off64_t,int);
#definefseeko64(fp,offset,whence)__mingw_fseeko64(fp,offset,whence)
#endif
__CRT_INLINEoff64_t__cdeclftello64(FILE*stream)
{
fpos_tpos;
if(fgetpos(stream,&pos))
return-1LL;
else
return((off64_t)pos);
}
#endif/*__NO_MINGW_LFS*/
#endif/*Not__STRICT_ANSI__*/
/*Wideversions*/
#ifndef_WSTDIO_DEFINED
/*alsoinwchar.h-keepinsync*/
_CRTIMPint__cdeclfwprintf(FILE*,constwchar_t*,...);
_CRTIMPint__cdeclwprintf(constwchar_t*,...);
_CRTIMPint__cdeclswprintf(wchar_t*,constwchar_t*,...);
_CRTIMPint__cdecl_snwprintf(wchar_t*,size_t,constwchar_t*,...);
_CRTIMPint__cdeclvfwprintf(FILE*,constwchar_t*,__VALIST);
_CRTIMPint__cdeclvwprintf(constwchar_t*,__VALIST);
_CRTIMPint__cdeclvswprintf(wchar_t*,constwchar_t*,__VALIST);
_CRTIMPint__cdecl_vsnwprintf(wchar_t*,size_t,constwchar_t*,__VALIST);
_CRTIMPint__cdeclfwscanf(FILE*,constwchar_t*,...);
_CRTIMPint__cdeclwscanf(constwchar_t*,...);
_CRTIMPint__cdeclswscanf(constwchar_t*,constwchar_t*,...);
_CRTIMPwint_t__cdeclfgetwc(FILE*);
_CRTIMPwint_t__cdeclfputwc(wchar_t,FILE*);
_CRTIMPwint_t__cdeclungetwc(wchar_t,FILE*);
#ifdef__MSVCRT__
_CRTIMPwchar_t*__cdeclfgetws(wchar_t*,int,FILE*);
_CRTIMPint__cdeclfputws(constwchar_t*,FILE*);
_CRTIMPwint_t__cdeclgetwc(FILE*);
_CRTIMPwint_t__cdeclgetwchar(void);
_CRTIMPwchar_t*__cdecl_getws(wchar_t*);
_CRTIMPwint_t__cdeclputwc(wint_t,FILE*);
_CRTIMPint__cdecl_putws(constwchar_t*);
_CRTIMPwint_t__cdeclputwchar(wint_t);
_CRTIMPFILE*__cdecl_wfdopen(int,wchar_t*);
_CRTIMPFILE*__cdecl_wfopen(constwchar_t*,constwchar_t*);
_CRTIMPFILE*__cdecl_wfreopen(constwchar_t*,constwchar_t*,FILE*);
_CRTIMPFILE*__cdecl_wfsopen(constwchar_t*,constwchar_t*,int);
_CRTIMPwchar_t*__cdecl_wtmpnam(wchar_t*);
_CRTIMPwchar_t*__cdecl_wtempnam(constwchar_t*,constwchar_t*);
_CRTIMPint__cdecl_wrename(constwchar_t*,constwchar_t*);
_CRTIMPint__cdecl_wremove(constwchar_t*);
_CRTIMPvoid__cdecl_wperror(constwchar_t*);
_CRTIMPFILE*__cdecl_wpopen(constwchar_t*,constwchar_t*);
#endif/*__MSVCRT__*/
#ifndef__NO_ISOCEXT/*externsinlibmingwex.a*/
int__cdeclsnwprintf(wchar_t*s,size_tn,constwchar_t*format,...);
__CRT_INLINEint__cdecl
vsnwprintf(wchar_t*s,size_tn,constwchar_t*format,__VALISTarg)
{return_vsnwprintf(s,n,format,arg);}
int__cdeclvwscanf(constwchar_t*__restrict__,__VALIST);
int__cdeclvfwscanf(FILE*__restrict__,
constwchar_t*__restrict__,__VALIST);
int__cdeclvswscanf(constwchar_t*__restrict__,
constwchar_t*__restrict__,__VALIST);
#endif
#define_WSTDIO_DEFINED
#endif/*_WSTDIO_DEFINED*/
#ifndef__STRICT_ANSI__
#ifdef__MSVCRT__
#ifndefNO_OLDNAMES
_CRTIMPFILE*__cdeclwpopen(constwchar_t*,constwchar_t*);
#endif/*notNO_OLDNAMES*/
#endif/*MSVCRTruntime*/
/*
*OtherNonANSIwidefunctions
*/
_CRTIMPwint_t__cdecl_fgetwchar(void);
_CRTIMPwint_t__cdecl_fputwchar(wint_t);
_CRTIMPint__cdecl_getw(FILE*);
_CRTIMPint__cdecl_putw(int,FILE*);
#ifndef_NO_OLDNAMES
_CRTIMPwint_t__cdeclfgetwchar(void);
_CRTIMPwint_t__cdeclfputwchar(wint_t);
_CRTIMPint__cdeclgetw(FILE*);
_CRTIMPint__cdeclputw(int,FILE*);
#endif/*Not_NO_OLDNAMES*/
#endif/*__STRICT_ANSI*/
#ifdef__cplusplus
}
#endif
#endif/*NotRC_INVOKED*/
#endif/*_STDIO_H_*/

stdio.h所包含的函数:

文件访问

fopen
freopen
fflush
fclose

二进制输入/输出

fread
fwrite

非格式化输入/输出

fgetc/getc
fputc/putc
ungetc
fgets
fputs

格式化输入/输出

scanf/fscanf/sscanf
printf/fprintf/sprintf
perror

文件定位

ftell
fseek
fgetpos
fsetpos
rewind

错误处理

feof
ferror

文件操作

remove
rename
tmpfile

本人第七次写文章,如有不足,请多多指教哦! 最后,动动小手,帮我点个赞吧!

如果认为作者的文章好的话,就打个赏吧!QQ号:2552972485

  • 40
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
引用\[1\]: stdio.h是C语言的标准I/O库,用于输入和输出字符。而在C++中,iostream是C++的标准I/O库,引入了输入/输出流的概念,并以类方法的形式进行字符的读取和写入。iostream.h是C++的非标准I/O库。为了避免命名冲突,C++引入了命名空间的概念。当使用iostream.h时,可以直接使用cout和cin进行输出和输入操作,而使用iostream时,需要使用命名空间std::或者使用using关键字引入std命名空间。其他头文件的使用方法也是类似的。\[1\] 问题: c++ stdio是什么? 回答: 在C++中,stdio不是一个特定的库或头文件,而是指"standard input & output"(标准输入输出)的缩写。在C语言或C++中,当需要使用标准输入输出函数时,需要包含stdio.h头文件。\[2\] #### 引用[.reference_title] - *1* [【C++学习(一)】iostream和iostream.h和stdio.h的区别](https://blog.csdn.net/Frances0905/article/details/122837576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [C++ stdio.h详解](https://blog.csdn.net/qq_56187979/article/details/117092179)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张耘嘉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值