《C++程序设计》作业8
一、单项选择题(共10小题,20分)
题型得分 14
关于 istream 类的getline 成员函数,以下说法正确的是( )。
(2分)
A. getline 只能读取一整行
B. getline 会往读入的内存缓冲区里自动添加’ ’
C. 用 getline 读取一行时,换行符也会被读入内存缓冲区
D. 用 getline 读取一行时,换行符还是留在输入流中
我的答案:
A
题目得分 0
参考答案:
B
若有以下程序
#include
#include
#include
using namespace std;
void WriteStr(string fn, string str) {
ofstream outFile;
outFile.open(fn.c_str());
outFile << str;
outFile.close();
}
int main() {
WriteStr(“t.dat”,“start”);
WriteStr(“t.dat”,“end”);
return 0;
}
程序运行后,文件t.dat中的内容是( )。
(2分)
A. end
B. start
C. startend
D. endrt
我的答案:
A
题目得分 2
参考答案:
A
ifstream 类的 read 成员函数的第一个参数的类型是( )。
(2分)
A. char *
B. const char *
C. int
D. char
我的答案:
A
题目得分 2
参考答案:
A
执行以下程序,输出结果为( )。
#include
#include
using namespace std;
int main() {
ofstream outFile;
outFile.open(“source.txt”);
for(int i = 0; i < 5; ++i)
outFile << i;
outFile.close();
ifstream inFile;
int m = 0, n = 0;
inFile.open(“source.txt”);
inFile >> m >> n;
inFile.close();
cout << m << “,” << n;
return 0;
}
(2分)
A. 0,0
B. 0,1
C. 1234,1
D. 1234,0
我的答案:
D
题目得分 2
参考答案:
D
关于文件,分类正确的是( )。
(2分)
A. 普通文件和设备文件
B. 顺序文件和随机文件
C. 以上全对
D. ASCII文件和二进制文件
我的答案:
D
题目得分 0
参考答案:
C
在已经包含头文件的情况下,下列文件操作的代码段在语法上正确的是( )。
(2分)
A.
int x = 0;
ifstream myfile;
myfile.open(“a”);
myfile << x;
if(myfile.eof())
cout << x << “: end of file” << endl;
B.
int x = 0;
ifstream myfile(‘a’);
myfile >> x;
if(myfile.eof())
cout << x << “: end of file” << endl;
C.
int x = 0;
ifstream myfile;
myfile.Open(“a”);
myfile >> x;
if(myfile.eof())
cout << x << “: end of file” << endl;
D.
int x = 0;
ifstream myfile(“a”);
myfile >> x;
if(myfile.eof())
cout << x << “: end of file” << endl;
我的答案:
D
题目得分 2
参考答案:
D
若文本文件f.txt中原有内容为:good,则运行下面程序后文件f.txt中的内容为( )。
#include
#include
using namespace std;
int main() {
ofstream outFile;
outFile.open(“f.txt”,ios::app);
outFile << “abc”;
outFile.close();
return 0;
}
(2分)
A. goodabc
B. abcd
C. abc
D. abcgood
我的答案:
A
题目得分 2
参考答案:
A
如果要打开文件,既进行读操作,又进行写操作,应该用( )类。
(2分)
A. iostream
B. fstream
C. ifstream
D. ofstream
我的答案:
B
题目得分 2
参考答案:
B
ofstream类的改变文件写指针位置的成员函数名为( )。
(2分)
A. tellp
B. tellg
C. seekp
D. seekg
我的答案:
A
题目得分 0
参考答案:
C
默认情况下,标准输出流对象“cout”直接关联的设备是( )。
(2分)
A. 打印机
B. 硬盘
C. 键盘
D. 显示器
我的答案:
D
题目得分 2
参考答案:
D
二、填空题(共10小题,20分)
题型得分 16
对于下面代码段:
string filename(“hello.txt”);
ifstream inFile(filename);
std::cout << inFle.fail();
若hello.txt文件不存在,输出________。
(2分)
我的答案:
true
题目得分 0
参考答案:
1
以下程序的输出是________。
#include
#include
using namespace std;
int main() {
ofstream outFile;
int i = 20, j = 30;
outFile.open(“example.txt”);
outFile << i << endl;
outFile << j << endl;
outFile.close();
ifstream inFile;
int m, n;
inFile.open(“example.txt”);
inFile >> m;
inFile >> n;
inFile.close();
cout << m << “#” << n << endl;
return 0;
}
(2分)
我的答案:
20#30
题目得分 2
参考答案:
20#30
下列程序的功能是计算并输出example.dat的文件________,以KB为单位。
#include
#include
using namespace std;
int main() {
ifstream inFile;
inFile.open(“example.dat”, ios::binary);
long size;
inFile.seekg(0L, ios::end);
size = inFile.tellg();
inFile.seekg(0L, ios::beg);
size -= inFile.tellg();
inFile.close();
cout << “size=” << size / 1024 <<“KB” << endl;
return 0;
}
(2分)
我的答案:
大小
题目得分 2
参考答案:
大小、长度
下面代码段打开一个文件。在________处填上恰当的成分,完成题目要求的功能。注意不需要任何空格。
……
ofstream outFile(“file.txt”);
if(________) {
cout<< “打开文件失败!” << endl;
exit(EXIT_FAILURE);
}
……
(2分)
我的答案:
!outFile.is_open()
题目得分 2
参考答案:
!outFile.is_open()、outFile.fail()、!outFile
下列程序的功能是统计并输出文件example.txt中________。
#include
#include
using namespace std;
int main() {
ifstream inFile;
inFile.open(“example.txt”);
int count = 0;
char ch;
inFile.get(ch);
while(!inFile.eof()) {
++count;
inFile.get(ch);
}
inFile.close();
cout << count << endl;
return 0;
}
(2分)
我的答案:
字符数
题目得分 2
参考答案:
字符个数、字符数
下面代码段打开了一个二进制文件。在________处填上恰当的成分,完成题目要求的功能。注意不需要任何空格。
……
ifstream inFile;
……
inFile.open(“abc.bmp”,________);
……
(2分)
我的答案:
ios::binary
题目得分 2
参考答案:
ios::binary
下列代码段的作用是逐行读取文本文件。在________处填上恰当的成分,完成题目要求的功能。注意不需要任何空格。
string line;
……
ifstream inFile(“file.txt”);
if(!inFile.is_open()) {
cout << “文件打开失败!” << endl;
exit(EXIT_FAILURE);
}
while(!inFile.eof()) {
________(inFile,line);
……
}
inFile.close();
……
(2分)
我的答案:
getline
题目得分 2
参考答案:
getline
下面代码段的作用是将字符串写入二进制文件。在________处填上恰当的成分,完成题目要求的功能。注意不需要任何空格。
string ss;
……
ofstream outFile(“file.dat”, ios::binary);
if(!outFile.is_open()) {
cout << “文件打开失败!” << endl;
exit(EXIT_FAILURE);
}
outFile.________((char *)ss.c_str(), sizeof(ss));
outFile.close();
……
(2分)
我的答案:
write
题目得分 2
参考答案:
write
若打开文件读取其内容,则读到文件末尾时调用eof()函数,该函数返回值为________。
(2分)
我的答案:
1
题目得分 0
参考答案:
true
下列程序执行后,文件example.txt中内容是________。
#include
#include
#include
using namespace std;
void fun(string filename, string str) {
ofstream outFile;
outFile.open(filename.c_str());
outFile << str;
outFile.close();
}
int main() {
fun(“example.txt”, “gelivable”);
fun(“example.txt”, “awesome”);
return 0;
}
(2分)
我的答案:
awesome
题目得分 2
参考答案:
awesome
单项选择题,第08套,第01题 总分 2 得分 0
详细:
错误 2 0
单项选择题,第08套,第02题 总分 2 得分 2
详细:
正确 2 2
单项选择题,第08套,第03题 总分 2 得分 2
详细:
正确 2 2
单项选择题,第08套,第04题 总分 2 得分 0
详细:
错误 2 0
单项选择题,第08套,第05题 总分 2 得分 2
详细:
正确 2 2
单项选择题,第08套,第06题 总分 2 得分 0
详细:
错误 2 0
单项选择题,第08套,第07题 总分 2 得分 2
详细:
正确 2 2
单项选择题,第08套,第08题 总分 2 得分 2
详细:
正确 2 2
单项选择题,第08套,第09题 总分 2 得分 2
详细:
正确 2 2
单项选择题,第08套,第10题 总分 2 得分 2
详细:
正确 2 2
填空题,第08套,第01题 总分 2 得分 2
详细:
正确 2 2
填空题,第08套,第02题 总分 2 得分 2
详细:
正确 2 2
填空题,第08套,第03题 总分 2 得分 2
详细:
正确 2 2
填空题,第08套,第04题 总分 2 得分 2
详细:
正确 2 2
填空题,第08套,第05题 总分 2 得分 0
详细:
错误 2 0
填空题,第08套,第06题 总分 2 得分 0
详细:
错误 2 0
填空题,第08套,第07题 总分 2 得分 2
详细:
正确 2 2
填空题,第08套,第08题 总分 2 得分 2
详细:
正确 2 2
填空题,第08套,第09题 总分 2 得分 2
详细:
正确 2 2
填空题,第08套,第10题 总分 2 得分 2
详细:
正确 2 2



