计算机二级

对于下面的程序 ifstream fin(c:\test.txt); if(fin) cout<<"ok"; else cout<<"wrong"; 对于if语句中的内容可以换为A.fin.fail( ):B.fin.bad( );C.fin.good( );D.fin.eof( );

题目

对于下面的程序 ifstream fin(c:\test.txt); if(fin) cout<<"ok"; else cout<<"wrong"; 对于if语句中的内容可以换为

A.fin.fail( ):

B.fin.bad( );

C.fin.good( );

D.fin.eof( );

参考答案和解析
正确答案:C
解析:C++语言中判断文件操作成功主要有两个函数good()和fail(),其中if文件流名. good())和if(!文件流名.fail()),还有if(文件流名)都可以表示判断文件操作是否成功。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

( 16 )要建立文件流并打开当前目录下的文件 file.dat 用于输入,下列语句中错误的是

A ) ifstream fin=ifstream.open ( "file.dat" ) ;

B ) ifstream*fir.=new ifstream ( "file.dat" ) ;

C ) ifstream fin; fin.open ( "file.dat" ) ;

D ) ifstream *fin=new ifstream ( ) ; fin 一 >open ( "file.dat" ) ;


正确答案:A

第2题:

简单C++问题。

#include<iostream.h>int main(){ int a; cout<<"请输入一个大于0的数:\n"; cin>>a; if (judge(a)==0){ cout<<"\n是偶数。"; }else{ cout<<"\n是奇数。"; }}int judge(int s){ if(s%2==0) { return 0; }else{ return 1; }}上面的程序哪里错了啊?为啥不能编译成功?


你把函数定义在main函数后面 在main函数中使用时 要调用 加句 #include<iostream.h> int main() { int judge(int);//这句声明要加的 int a; cout<<"请输入一个大于0的数:\n"; cin>>a; if (judge(a)==0){ cout<<"\n是偶数。"; }else{ cout<<"\n是奇数。"; } } int judge(int s){ if(s%2==0) { return 0; }else{ return 1; } }

第3题:

若磁盘上已存在某个文本文件,其全路径文件名为 d:\ncre\test.txt ,下列语句中不 能打开该文件的是

A . ifstream file("d:\ncre\test.txt") ;

B . ifstream file("d:\ncre\test.txt");

C . ifstream file; file.open("d:\ncre\test.txt");

D . ifstream* pFile=new ifstream("d:\ncre\test.txt");


正确答案:A

 

第4题:

若语句:cout<')<

若语句: cout<<seffill('>')<<setw(5)<<3141512<<sctw(5)<<"OK!"; 是程序中第1个输出语句,则输出结果是( )。

A.3141512>>OK!

B.31415120K!>>

C.314150K!>>

D.31415>>OK!


正确答案:A

第5题:

有如下程序:include include using namespace std;int main(){cout.fill('

有如下程序:#include <iostream>#include <iomanip>using namespace std;int main(){cout.fill('*');cout<<left<<setw(4)<<123<< "OK"<<end1;return 0;}执行这个程序的输出结果是( )。

A.123*OK

B.123*OK**

C.*123OK

D.*123**OK


正确答案:A

第6题:

下面的程序的结果是

main( )

{int x=3,y=0, z=0;

if(x=y+z)cout<<"* * * *";

else cout<<"####";

}

A.有语法错误不能通过编译

B.输出****

C.可以通过编译,但是不能通过连接,因而不能运行

D.输出####


正确答案:D
解析:注意本题本意是考察x=y+z,但是少写了一个=,因此逻辑表达式变成了赋值语句,故x的值为0,即假,因此程序执行else部分。本题答案为D。

第7题:

下列程序从保存整数的文本文件”c:Sample.dat”中依次取出每个数据并显示出来,同时统计并显示出所有数据的个数。程序划线处的表达式为______。

include<stream.h>

include<stdlib.h>

void main( )

{

ifstream fin("c:Sample.dat",los::nocreate);

if(! fin)

{

cout<<"文件无法打开!"<<endl;

exit(1);

}

int x,i=0;

while(______)

{

cout<<x<<"";

i++;

}

fin.close( );

cout<<endl<<"文件中所有整数个数:"<<i<<endl;

}


正确答案:fin>>x
fin>>x 解析:while语句用于完成题目指定的功能,则while的条件判断部分应完成从文件读取字符的功能,并能够判断出读入字符失败后的情况。

第8题:

若语句: cout<')<

若语句: cout<<setfill('>')<<setw(5)<<3141512<<setw(5)<<"OK!"; 是程序中第一个输出语句,则输出结果是

A.3141512>>OK!

B.31415120K!>>

C.414150K!>>

D.31415>>OK!


正确答案:A

第9题:

对于下面的程序: ifstream fin(c:\test.txt) ; if(fin) cout < < "ok"; else cout < < "wrong"; if语句中的内容可以换为

A.fin. fail( ) ;

B.fin. bad( ) ;

C.fin. good( ) ;

D.fin. eof( ) ;


正确答案:C
解析:C++语言中判断文件操作成功主要有两个函数good( ) 和fail( ) ,其中if(文件流名. good( ) ) 和if(!文件流名.fail( ) ),还有if(文件流名) 都可以表示判断文件操作是否成功。

第10题:

以下程序的输出结果是()。includemain(){int m=5;if(m++>5)cout<

以下程序的输出结果是( )。 #include<iostream.h> main() { int m=5; if(m++>5) cout<<m; else cout<<m--; }

A.7

B.6

C.5

D.4


正确答案:B

更多相关问题