安全区的英文翻译英语怎么说-四年级英语教学计划
2023年3月31日发(作者:简体字转繁体字)
Cerr
Cstdlib
Fstream
Ctype
#ifndef
条件编译,是让编译器在条件满足时才进行编译,否则编译器就会忽略。
条件编译块,其语法结构为:
#if[必须]
#elif[可选]
#else[可选]
#endif[必须]
另外下面的形式是等价的:
#ifdef(identifer)<==>#ifdefined(identifer)
#ifndef(identifer)<==>#if!defined(identifer)
其作用依赖于你的写法和意图,例如:
(1)防止头文件被重复编译,在头文件windows.h中我们看到一个最大的条件
编译块:
#ifndef_WINDOWS_//位于头文件有效代码最顶部
#define_WINDOWS_
//...
#过故人庄的过是什么意思 endif//位于头文件尾部
那么如果你的工程实现文件中重复包含了windows.h也不会导致问题,因为
windows.h在被第一次编译的时候就定义了_WINDOWS_标识,下次编译器不
会再编译条件编译块之间的代码了,因为条件为假。
(2)检测CPU类型
#if!defined(_68K_)&&!defined(_MPPC_)&&!defined(_X86_)
&&!defined(_IA64_)&&!defined(_AMD64_)&&defined(_M_IX86)
#define_X86_//Intel公司提供的X86系列CPU
#endif
#if!defined(_68K_)&&!defined(_MPPC_)&&!defined(_X86_)
&&!defined(_IA64_)&&!defined(_AMD64_)&&defined(_M_AMD64)
#define_AMD64_//ADM公司提供的CPU
#endif
#if!defined(_68K_)&&!defined(_MPPC_)&&!defined(_X86_)
&&!defined(_IA64_)&&!defined(_AMD64_)&&defined(_M_M68K)
#define_68K_//苹果电脑通常使用一种CPU
#endif
#if!defined(_68K_)&&!defined(_MPPC_)&&!defined(_X86_)
&&!defined(_IA64_)&&!defined(_AMD64_)&&defined(_M_MPPC)
#define_MPPC_//苹果电脑通常使用另一种CPU
#endif
(3)检测硬件平台
#ifndef_MAC
#ifdefined(_68K_)||defined(_MPPC_)//如果使用了这两种类型的CPU
#define_MAC//则为苹果电脑的硬件平台
#endif
#endif
所以楼主的理解基本是对的,当然条件编译块的用途实在是太多了,你当然可以
把它嵌入你的源代码中,控制编译器有选择地编译,而不是仅仅在头文件中使用。
我要指出楼主一点小小的错误:
如果定义了_MAC标识,是指的苹果电脑的硬件平台,而不是苹果操作系统。事
实上,苹果电脑上现在也可以装windows操作系统。
要知道编译器对不同的硬件平台(主要指CPU类型)生成的机器码是不同的,所
以界定硬件平台是重要的,但是由于我们基本上都是使用的X86系列CPU,所以
实际也不需要太关心这个
cerr
cerr与cout的主要区分就是,cout输出的信息可以重定向,而cerr
只能输出到标准输出(显示器)上。
例如下面程序编译后生成
//
#include
intmain()
{
cout<<\"helloworld---cout\"<
cerr<<\"helloworld---cerr\"<
return0;
}
在命令行模式下键入下面的命令:
test>>
运行结果是:
在生成的文件中输出了\"helloworld---cout\"
同时在显示器上输出了\"helloworld---cerr\"
也就是说cout的输出可以重定向到一个文件中,而cerr必须输出在
显示器上。
cout是标准输出流,与cerr的区别在于cerr不经过缓冲区,直接向显
示器输出信息,而cout中的信息存放在缓冲区,缓冲区满或者遇到endl时
才输出.
对于为什么有cerr和cout
比如,你的程序遇到调用栈用完了的威胁(无限,没有出口的递归)。
你说,清平乐 村居诗意解释 你到什么地方借内存,存放你的错误信息?
所以有了cerr。其目的,就是在你最需要它的紧急情况下,还能得到
输出功能的支持。
缓冲区的目的,就是减少刷屏的次数——比如,你的程序输出圣经中
的一篇文章。不带缓冲的话,就会每写一个字母,就输出一个字母,然后
刷屏。有了缓冲,你将看到若干句子“同时”就出现在了屏幕上(由内存
翻新到显存,然后刷新屏幕)。
Cstdlib
cstdlib是C++里面的一个常用函数,等价于C中的
C++中常用的头文件之一。
使用:#include
该函数主要可以提供一些函数与符号常量,具体如下:
size_t,wchar_t,div_t,ldiv_t,lldiv_t
常量
NULL,EXIT_FAILURE,EXIT_SUCESS,RAND_MAX,MB_CUR_MAX
函数
atof,atoi,atol,strtod,strtof,strtols,strtol,strtoll,strtoul,strtoull,rand,srand,callc,
free,maloc,realloc,abort,atexit,exit,getenv,system,bsearch,qsort,abs,div,labs,ldiv,
llabs,tlldiv,mblen,mbtowc,wctomb,mbstowcs,wcstombs
Fstream
需要包含的头文件:
名字空间:std
也可以试用
fstream提供了三个类,用来实现c++对文件的操作。(文件的创建,读写)。
ifstream--从已有的文件读
ofstream--向文件写内容
fstream-打开文件供读写
支持的文件类型
实际上,文件类型可以分为两种:文本文件和二进制文件.
文本文件保存的是可读的字符,而二进制文件保存的只是二进制数据。利用二进制模
式,你可以操作图像等文件。用文本模式,你只能读写文本文件。否则会报错。
例一:写文件
声明一个ostream变量
调用open方法,使其与一个文件关联
写文件
调用close方法.
#include
voidmain
{
ofstreamfile;
(\"\");
file<<\"Hellofile/n\"<<75;
();
}
可以像试用cout一样试用操作符<<向文件写内容.
Usages:
file<<\"string/n\";
(’c’);
例二:读文件
1.声明一个ifstream变量.
2.打开文件.
3.从文件读数据
4.关闭文件.
#include
voidmain
{
ifstreamfile;
charoutput[100];
intx;
(\"\");
file>>output;
cout<
file>>x;
cout<
();
}
同样的,你也可以像cin一样使用>>来操作文件。或者是调用成员函数
Usages:
file>>char*;
file>>char;
(char);
(char*,int);
e(char*,intsz);
e(char*,intsz,chareol);
1.同样的,你也可以使用构造函数开打开一个文件、你只要把文件名作为构造函数的
第一个参数就可以了。
ofstreamfile(\"\");
ifstreamfile(\"\");
上面所讲的ofstream和ifstream只能进行读或是写,而fstream则同时提供读写的功
能。
voidmain()
{
fstreamfile;
(\"\",iso::in|ios::out)
//doaninputoroutputhere
();
}
open函数的参数定义了文件的打开模式。总共有如下模式
属性列表
ios::in读
ios::out写
ios::app从文件末尾开始写
ios::binary二进制模式
ios::nocreate打开一个文件时,如果文件不存在,不创建文件。
ios::noreplace打开一个文件时,如果文件不存在,创建该文件
ios::trunc打开一个文件,然后清空内容
ios::ate打开一个文件时,将位置移动到文件尾
Notes
默认模式是文本
默认如果文件不存在,那么创建一个新的
多种模式可以混合,用|(按位或)
文件的byte索引从0开始。(就像数组一样)
我们也可以调用read函数和write函数来读写文件。
文件指针位置在c++中的用法:
ios::beg文件头
ios::end文件尾
ios::cur当前位置
例子:
(
=\"nu0\">0,ios::end);
intfl_sz=();
(0,ios::beg);
常用的错误判断方法:
good()如果文件打开成功
bad()打开文件时发生错误
eof()到达文件尾
例子:
charch;
ifstreamfile(\"\",ios::in|ios::out);
if(())cout<<\"Thefilehasbeenopenedwithoutproblems;
elsecout<<\"AnErrorhashappendonopeningthefile;
while(!())
{
file>>ch;
cout<
}
ctype.h
ctype.h里的函数
1字符测试函数
1>函数原型均为intisxxxx(int)
2>参数为int,任何实参均被提升成整型
3>只能正确处理处于[0,127]之间的值
2字符映射函数
1>函数原型为inttoxxxx(int)
2>对参数进行检测,若符合范围则转换,否则不变
inttolower(int);\'A\'~\'Z\'==>\'a\'~\'z\'
inttoupper(int);\'a\'~\'z\'==>\'A\'~\'Z\'
@函数名称:isalpha
函数原型:intisalpha(intch);
函数功能:检查ch是否是字母.
函数返回:是字母返回非0,否则返回0.
参数说明:
所属文件
#include
#include
intmain()
{
charch1=\'*\';
charch2=\'a\';
if(isalnum(ch1)!=0)
printf(\"%cistheASCIInumberoralphebetn\",ch1);
else
printf(\"%cisnottheASCIInumbernoralphebetn\",ch1);
if(isalnum(ch2)!=0)
printf(\"%cistheASCIInumberoralphebetn\",ch2);
else
printf(\"%cisnottheASCIInumbernoralphebetn\",ch2);
return0;
}
@函数名称:iscntrl
函数原型:intiscntrl(intch);
函数功能:检查ch是否控制字符(其ASCII码在0和0x1F之间,数值
为0-31).
函数返回:是返回1,否则返回0.
参数说明:
所属文件:
#include
#include
charchars[]={\'A\',登快阁朗读 0x09,\'Z\'};
#defineSIZEsizeof(chars)/sizeof(char)
intmain()
{
inti;
for(i=0;i
printf(\"Char%cis%saControlcharactern\",
chars[i],(iscntrl(chars[i]))?\"\":\"not\");
}
return0;
}
@函数名称:isdigit
函数原型:intisdigit(intch);
函数功能:检查ch是否是数字(0-9)
函数返回:是返回1,否则返回0
参数说明:
所属文件:
#include
#include
intmain()
{
charch1=\'1\';
charch2=\'a\';
if(isdigit(ch1)!=0)
printf(\"%cistheASCIInumbern\",ch1);
else
printf(\"%cisnottheASCIInumbern\",ch1);
if(isdigit(ch2)!=0)
printf(\"%cistheASCIInumbern\",ch2);
else
printf(\"%cisnottheASCIInumbern\",ch2);
return0;
}
@函数名称:isgraph
函数原型:intisgraph(intch);
函数功能:检查ch是否可显示字符(其ASCII码在ox21到ox7E之间),
不包括空格
函数返回:是返回1,否则返回0
参数说明:
所属文件:
#include
#include
intmain()
{
charch1=\'\';
charch2=\'a\';
if(isgraph(ch1)!=0)
printf(\"%cistheASCIIprintablecharactern\",ch1);
else
printf(\"%cisnottheASCIIprintablecharactern\",ch1);
if(isgraph(ch2)!=0)
printf(\"%cistheASCIIprintablecharactern\",ch2);
else
printf(\"%cisnottheASCIIprintablecharactern\",ch2);
return0;
}
@函数名称:islower
函数原型:intislower(intch);
函数功能:检查ch是否小写字母(a-z)
函数返回:是返回1,否则返回0
参数说明:
所属文件:
#include
#include
charchars[]={\'A\',\'a\',\'z\',\'Z\'};
#defineSIZEsizeof(chars)/sizeof(char)
intmain()
{
inti;
for(i=0;i
printf(\"Char%cis%salowercase
charactern\",chars[i],(islower(chars[i]))?\"\":\"not\");
}
return0;
}
@函数名称:tolower
函数原型:inttolower(intch);
函数功能:将ch字符转换为小写字母
函数返回:返回ch所代表的字符的小写字母
参数说明:
所属文件:
#include
#include
#include
intmain()
{
charx=\'a\',y=\'b\',z=\'A\',w=\'*\';
printf(\"Character%ctoloweris%cn\",x,t万户萧疏鬼唱歌 olower(x));
printf(\"Character%ctoloweris%cn\",y,tolower(y));
printf(\"Character%ctoloweris%cn\",z,tolower(z));
printf(\"Character%ctoloweris%cn\",w,tolower(w));
return0;
}
@函数名称:toupper
函数原型:inttoupper(intch);
函数功能:将ch字符转换成大写字母
函数返回:与ch相应的大写字母
参数说明:
所属文件:
#include
#include
#include
intmain()
{
charx=\'a\',y=\'b\',z=\'A\',w=\'*\';
printf(\"Character%ctoupperis%cn\",x,toupper(x));
printf(\"Character%ctoupperis%cn\",y,toupper(y));
printf(\"Character%ctoupperis%cn\",z,toupper(z));
printf(\"Character%ctoupperis%cn\",w,toupper(w));
return0;
}
@函数名称:isalnum
函数原型:intisalnum(intch);
函数功能:检查ch是否是字母或数字
函数返回:是字母或数字返回1,否则返回0
参数说明:
所属文件:
#include
#include
intmain()
{
charch1=\'*\';
charch2=\'a\';
if(isalnum(ch1)!=0)
printf(\"%cistheASCIInumberoralphebetn\",ch1);
else
printf(\"%cisnottheASCIInumbernoralphebetn\",ch1);
if(isalnum(ch2)!=0)
printf(\"%cistheASCIInumberoralphebetn\",ch2);
else
printf(\"%cisnottheASCIInumbernoralphebetn\",ch2);
return0;
}
@函数名称:isprint
函数原型:intisprint(intch);
函数功能:检查ch是否是可打印字符(包括空格),其ASCII码在ox20
到ox7E之间
函数返回:是返回1,否则返回0
参数说明:
所属文件:
#include
#include
intmain()
{
charch1=\'n\';
charch2=\'a\';
if(isprint(ch1)!=0)
printf(\"%cistheASCIIprintablecharcatern\",ch1);
else
printf(\"%cisnottheASCIIprintablecharcatern\",ch1);
if(isprint(ch2)!=0)
printf(\"%cistheASCIIprintablecharcatern\",ch2);
else
printf(\"%cisnottheASCIIprintablecharcatern\",ch2);
return0;
}
@函数名称:ispunct
函数原型:intispunct(intch);
函数功能:检查ch是否是标点字符(不包括空格),即除字母,数字和空
格以外的所有可打印字符
函数返回:是返回1,否则返回0
参数说明:
所属文件:
#include
#include
intmain()
{
charch1=\',\';
charch2=\'a\';
if(ispunct(ch1)!=0)
printf(\"%cistheASCIIpunctn\",ch1);
else
printf(\"%cisnottheASCIIpunctn\",ch1);
if(ispunct(ch2)!=0)
printf(\"%cistheASCIIpunctn\",ch2);
else
printf(\"%cisnottheASCIIpunctn\",ch2);
return0;
}
@函数名称:isspace
函数原型:intisspace(intch);
函数功能:检查ch是否是空格符和跳格符(控制字符)或换行符
函数返回:是返回1,否则返回0
参数说明:
所属文件:
#include
#include
intmain()
{
charch1=\'\';
charch2=\'a\';
if(isspace(ch1)!=0)
printf(\"%cisthespacecharcatern\",ch1);
else
printf(\"%cisnotthespacecharcatern\",ch1);
if(isspace(ch2)!=0)
printf(\"%cisthespacecharcatern\",ch2);
else
printf(\"%cisnotthespacecharcatern\",ch2);
return0;
}
@函数名称:isupper
函数原型:intisupper(intch);
函数功能:检查ch是否是大写字母(A-Z)
函数返回:是返回1,否则返回0
参数说明:
所属文件:
#include
#include
charchars[]={\'A\',\'a\',\'z\',\'Z\'};
#defineSIZEsizeof(chars)/sizeof(cha中秋节赏月古诗 r)
intmain()
{
inti;
for(i=0;i
printf(\"Char%cis%sanuppercasecharactern\",
chars[i],(isupper(chars[i]))?\"\":\"not\");
}
return0;
}
@函数名称:isxdigit
函数原型:intisxdigit(intch);
函数功能:检查ch是否是一个16进制数学字符(即0-9,或A-F,或a-f)
函数返回:是返回1,否则返回0
参数说明:
所属文件:
#include
#include
intmain()
{
charch1=\'1\';
charch2=\'a\';
if(isxdigit(ch1)!=0)
printf(\"%cistheASCIIhexadecimalnumbern\",ch1);
else
printf(\"%cisnottheASCIIhexadecimalnumbern\",ch1);
if(isxdigit(ch2)!=0)
printf(\"%cistheASCIIhexadecimalnumbern\",ch2);
else
printf(\"%cisnottheASCIIhexadecimalnumbern\",ch2);
return0;
}
@函数名称:isascii
函数原型:intisascii(intch)
函数功能:测试参数是否是ASCII码0-127
函数返回:非零-是,0-不是
参数说明:ch-被测参数
所属文件:
#include
#include
charchars[]={\'A\',0x80,\'Z\'};
#defineSIZEsizeof(chars)/sizeof(char)
intmain()
{
inti;
for(i=0;i
{
printf(\"Char%cis%sanASCIIcharactern\",
chars[i],(isascii(chars[i]))?\"\":\"not\");
}
return0;
}
isalpha
函数:isalpha
原型:intisalpha(intch)
用法:头文件加入#include
功能:判断字符ch是否为英文字母,当ch为英文字母a-z或A-Z时,
在标准c中相当于使用“isupper(ch)||islower(ch)”做测试,返回非零
值(不一定是1),否则返回零。
PS:{
isupper
原型:externintisupper(intc);
头文件:
功能:判断字符c是否为大写英文字母
说明:当参数c为大写英文字母(A-Z)时,返回非零值,否则返回零。
附加说明:此为宏定义,非真正函数。
islower
islower(测试字符是否为小写字母)
相关函数
isalpha,isupper
表头文件
#include
定义函数
intislower(intc)
函数说明
检查参数c是否为小写英文字母。
返回值
若参数c为小写英文字母,则返回TRUE,否则返回NULL(0)。
附加说明:此为宏定义,非真正函数。
}
示例:
/*本函数运行环境VisualC++6.0,测试结果:通过*/
#include
#include
intmain(void)
{
charch;
inttotal;
total=0;//初始化
/*统计字母块*/
do
{
ch=getchar();
if(isalpha(ch)!=0)
total++;
}while(ch!=\'.\');//结束符号为.
printf(\"Thetotaloflettersis%dn\",total);
return0;
}
/*运行结果*/
输入:123456我am侯云江.
输出:Thetotaloflettersis2
fflush
函数名:fflush
功能:清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件
原型:intfflush(FILE*stream)
程序例:
#include
#include
#include
#include
voidflush(FILE*stream);
intmain(void)
{
FILE*stream;
charmsg[]=\"Thisisatest\";
/*createafile*/
streaai人工智能写诗平台 m=fopen(\"\",\"w\");
/*writesomedatatothefile*/
fwrite(msg,strlen(msg),1,stream);
clrscr();
printf(\":\");
getch();
/*hout
closingit*/
flush(stream);
printf(\"nFilewasflushed,Pressanykey
toquit:\");
getch();
return0;
}
voidflush(FILE*stream)
{
intduphandle;
/*flushthestream\'sinternalbuffer*/
fflush(stream);
/*makeaduplicatefilehandle*/
duphandle=dup(fileno(stream));
/*closetheduplicatehandletoflushtheDOSbuffer*/
close(duphandle);
}
fflush的返回值类型是int类型,那么这个int类型具体的返回是什么呢?
返回值:
如果成功刷新,fflush返回0。指定的流没有缓冲区或者只读打开时也返回0值。
返回EOF指出一个错误。
注意:如果fflush返回EOF,数据可能由于写错误已经丢失。当设置一个重要错误
处理器时,最安全的是用setvbuf函数关闭缓冲或者使用低级I/0例程,如open、close
和write来代替流I/O函数。
fflush()函数
fflush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃
fflush(stdout)刷新标准输出缓冲区,把输出缓冲区里的东西打印到标准输出设备
上
注意事项:
C和C++的标准里从来没有定义过fflush(stdin)。也许有人会说:“可是我用
fflush(stdin)解决了这个问题,你怎么能说是错的呢少壮不努力的下一句 ?”的确,某些编译器(如VC6)
支持用fflush(stdin)来清空输入缓冲,但是并非所有编译器都要支持这个功能(linux
下的gcc就不支持),因为标准中根本没有定义fflush(stdin)。
linux下的GCC是支持fflush(stdin)的。
更多推荐
openfile是什么意思nfile在线翻译读音例
发布评论