c++流類庫與輸入輸出習(xí)題答案_第1頁
c++流類庫與輸入輸出習(xí)題答案_第2頁
c++流類庫與輸入輸出習(xí)題答案_第3頁
c++流類庫與輸入輸出習(xí)題答案_第4頁
c++流類庫與輸入輸出習(xí)題答案_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、1.概念填空題1.1頭文件iostream中定義了 4個標(biāo)準(zhǔn)流對象 cin , cout , cerr , clog 。其中標(biāo)準(zhǔn)輸入流對象為 cin ,與 鍵盤 連用,用于輸入; cout 為標(biāo)準(zhǔn)輸出流對象,與顯示器 連用,用于輸出。1.2用標(biāo)準(zhǔn)輸入流對象cin與提取操作符>>連用進(jìn)行輸入時,將 空格 與 回車 當(dāng)作分隔符,使用 get() 成員函數(shù)進(jìn)行輸入時可以指定輸入分隔符。1.3每一個輸入輸出流對象都維護(hù)一個 流格式狀態(tài)字 ,用它表示流對象當(dāng)前的格式狀態(tài)并控制流的格式。C+提供了使用 格式控制 函數(shù)與 操作子 函數(shù)來控制流的格式的方法。1.4 C+根據(jù)文件內(nèi)容的 數(shù)據(jù)格式 可

2、分為兩類: 文本文件 和 二進(jìn)制文件 。前者存取的最小信息單位為 字節(jié) ,后者 記錄 。1.5文件輸入是指從文件向 內(nèi)存 讀入數(shù)據(jù);文件輸出則指從 內(nèi)存 向文件輸出數(shù)據(jù)。文件的輸入輸出首先要 打開文件 ;然后 進(jìn)行讀寫 ;最后 關(guān)閉文件 。1.6文本文件是存儲ASCII碼字符的文件,文本文件的輸入可用 cin 從輸入文件流中提取字符實(shí)現(xiàn)。文本文件的輸出可用 cout 將字符插入到輸出文件流來實(shí)現(xiàn)。程序在處理文本文件時 需要 (需要/不需要)對數(shù)據(jù)進(jìn)行轉(zhuǎn)換。1.7二進(jìn)制文件是指直接將計算機(jī)內(nèi)的數(shù)據(jù)不經(jīng)轉(zhuǎn)換直接保存在文件中。二進(jìn)制文件的輸入輸出分別采用read()、write() 成員函數(shù)。 這兩

3、個成員函數(shù)的參數(shù)都是 2個,分別表示 讀寫緩沖區(qū) 和 字節(jié)數(shù) 。1.8設(shè)定、返回文件讀指針位置的函數(shù)分別為 seekg , tellg ;設(shè)定、返回文件寫指針位置的函數(shù)分別為 seekp , tellp 。2 簡答題2.1 為什么cin輸入時,空格和回車無法讀入?這時可改用哪些流成員函數(shù)?2.2 文件的使用有它的固定格式,試做簡單介紹。2.3 在ios類中定義的文件打開方式中,公有枚舉類型open_mode的各成員代表什么文件打開方式?2.4 簡述文本文件和二進(jìn)制文件在存儲格式、讀寫方式等方面的不同,各自的優(yōu)點(diǎn)和缺點(diǎn)。2.5 文本文件可以按行也可以按字符進(jìn)行復(fù)制,在使用中為保證能完整復(fù)制要注意

4、哪些問題?2.6 文件的隨機(jī)訪問為什么總是用二進(jìn)制文件,而不用文本文件?2.7 怎樣使用istream和ostream的成員函數(shù)來實(shí)現(xiàn)隨機(jī)訪問文件?3選擇題3.1要進(jìn)行文件的輸出,除了包含頭文件iostream外,還要包含頭文件(C )。 AifstreamBfstreamCostreamDcstdio3.2執(zhí)行以下程序:char *str;cin>>str;cout<<str;若輸入abcd 1234則輸出(D)。 AabcdBabcd 1234 C1234D輸出亂碼或出錯3.3執(zhí)行下列程序:char a200;cin.getline(a,200, );cout<

5、;<a;若輸入abcd 1234則輸出(A)。 AabcdBabcd 1234 C1234D輸出亂碼或出錯3.4定義char *p=”abcd”,能輸出p的值(“abcd”的地址)的為(A /D ?)。 Acout<<&p;Bcout<<p; Ccout<<(char*)p; Dcout<<const_cast<void *>(p);3.5以下程序執(zhí)行結(jié)果(A)。cout.fill(#);cout.width(10);cout<<setiosflags(ios:left)<<123.456; A1

6、23.456#B123.4560000 C#123.456D123.4563.6當(dāng)使用ifstream定義一個文件流,并將一個打開文件的文件與之連接,文件默認(rèn)的打開方式為(A)。 Aios:inBios:outCios:trunc Dios:binary3.7從一個文件中讀一個字節(jié)存于char c;正確的語句為(B)。 Afile.read(reinterpret_cast<const char *>(&c), sizeof(c); Bfile.read(reinterpret_cast<char *>(&c), sizeof(c); Cfile.rea

7、d(const char *)(&c), sizeof(c); Dfile.read(char *)c, sizeof(c);3.8將一個字符char c=A寫到文件中錯誤的語句為(D)。 Afile.write(reinterpret_cast<const char *>(&c), sizeof(c); Bfile.write(reinterpret_cast<char *>(&c), sizeof(c); Cfile.write(char *)(&c), sizeof(c); Dfile.write(const char *)c, s

8、izeof(c);3.9讀文件最后一個字節(jié)(字符)的語句為(B)。 Amyfile.seekg(1,ios:end);Bmyfile.seekg(-1,ios:end); c=myfile.get(); c=myfile.get(); Cmyfile.seekp(ios:end,0); Dmyfileseekp(ios:end,1); c=myfile.get(); c=myfile.get();3.10 read函數(shù)的功能是從輸入流中讀取(D )。 A.一個字符 B.當(dāng)前字符 C.一行字符 D.指定若干字節(jié)3.11 要求打開文件D:file.dat,并能夠?qū)懭霐?shù)據(jù),正確的語句是( D )。

9、A.ifstream infile("D:file.dat", ios:in); B. ifstream infile("D:file.dat", ios:out); C. ofstream outfile("Dfile.dat", ios:in); D.fstream infile("Dfile.dat", ios:in | ios:out);3.12 設(shè)己定義浮點(diǎn)型變量data,以二進(jìn)制方式把data的值寫入輸出文件流對象outfile中去,正確的每句是( C )。 A. outfile.write(doubl

10、e*)&data, sizeof (double); B.outfile.write(double*)&data, data); C. outfile.write(char*)&data, sizeof (double); D. outfile.write(char*)&data, data);4編程題4.1 編程實(shí)現(xiàn)以下數(shù)據(jù)輸入/輸出: (1)以左對齊方式輸出整數(shù),域?qū)挒?2。 (2)以八進(jìn)制、十進(jìn)制、十六進(jìn)制輸入/輸出整數(shù)。 (3)實(shí)現(xiàn)浮點(diǎn)數(shù)的指數(shù)格式和定點(diǎn)格式的輸入/輸出,并指定精度。 (4)把字符串讀入字符型數(shù)組變量中,從鍵盤輸入,要求輸入串的空格也全部

11、讀入,以回車符結(jié)束。 (5)將以上要求用流成員函數(shù)和流操作子各做一遍。Ios類成員函數(shù)實(shí)現(xiàn)#include<iostream>#include<iomanip>using namespace std;int main()long a=234;double b=2345.67890;char c100;cout.fill('*');cout.flags(ios_base:left);cout.width(12);cout<<a<<endl;cout.fill('*');cout.flags(ios:right);co

12、ut.width(12);cout<<a<<endl;cout.flags(ios.hex);cout<<234<<'t'cout.flags(ios.dec);cout<<234<<'t'cout.flags(ios.oct);cout<<234<<endl;cout.flags(ios:scientific);cout<<b<<'t'cout.flags(ios:fixed);cout<<b<<end

13、l;cin.get(c,99);cout<<c<<endl;return 0;流操作子實(shí)現(xiàn)#include<iostream>#include<iomanip>using namespace std;int main()long a=234;double b=2345.67890;char c100;cout<<setfill('*');cout<<left<<setw(12)<<a<<endl;cout<<right<<setw(12)<&

14、lt;a<<endl;cout<<hex<<a<<'t'<<dec<<a<<'t'<<oct<<a<<endl;cout<<scientific<<b<<'t'<<fixed<<b<<endl;return 0;4.2編寫一程序,將兩個文件合并成一個文件。#include<iostream>#include<fstream>using

15、 namespace std;int main()int i=1;char c1000;ifstream ifile1("D:10_4_3.cpp");ifstream ifile2("D:10_4_4.cpp");ofstream ofile("D:r10_4.cpp");while(!ifile1.eof()ifile1.getline(c,999);ofile<<c<<endl;while(!ifile2.eof()ifile2.getline(c,999);ofile<<c<<e

16、ndl;ifile1.close();ifile2.close();ofile.close();return 0;4.3編寫一程序,統(tǒng)計一篇英文文章中單詞的個數(shù)與行數(shù)。#include<iostream>#include<fstream>using namespace std;bool isalph(char);int main()ifstream ifile("E:lenovoenglishdaily.doc");char text1000;bool inword=false;int rows=0,words=0;int i;while(!ifil

17、e.eof()ifile.getline(text,999);rows+;i=0;while(texti!=0)if(!isalph(texti)inword=false;else if(isalph(texti) && inword=false)words+;inword=true;i+;cout<<"rows= "<<rows<<endl;cout<<"words= "<<words<<endl;ifile.close ();return 0;bool isalp

18、h(char c)return (c>='A' && c<='Z') | (c>='a' && c<='z');4.4編寫一程序,將C+源程序每行前加上行號與一個空格。#include<iostream>#include<fstream>using namespace std;int main()int i=1;char c1000;ifstream ifile("D:10_4_3.cpp");ofstream ofile(&quo

19、t;D:r10_4_3.cpp");while(!ifile.eof()ofile<<i+<<": "ifile.getline(c,999);ofile<<c<<endl;ifile.close();ofile.close();return 0;4.5編寫一程序,輸出 ASCII碼值從20到127的ASCII碼字符表,格式為每行10個。#include<iostream>using namespace std;int main()int i,l;for(i=32;i<127;i+)cout<

20、<char(i)<<" "l+;if(l%10=0)cout<<endl;cout<<endl;return 0;4.6將第8章習(xí)題7.6 ClockwithDate類對象以”年-月-日 時:分:秒”格式,以文本形式輸出之文件,并對插入操作符<<進(jìn)行重載。ostream& operator<<(ostream& out,ClockwithDate& CD)out<<CD.Year<<"年"<<CD.Month<<&qu

21、ot;月"<<CD.Day<<"日 "<<CD.Hour<<":"<<CD.Minute<<":"<<CD.Second;out<<(CD.Ap=1 ? "上午" : "下午")<<endl;return out;4.7定義一個Student類,其中含學(xué)號、姓名、成績數(shù)據(jù)成員。建立若干個Student類對象,將它們保存到文件Record.dat中。然后顯示文件中的內(nèi)容。#incl

22、ude <iostream.h> #include <string.h>#include <fstream.h>/using namespace std; class Studentchar Id10;char Name10;int Score;public:Student();Student(char *id,char *name,int score);Student(Student &s);Student();friend ostream& operator<<(ostream&,Student&);Stude

23、nt:Student()strcpy(Id,"00000000");strcpy(Name,"noname");Score=-1;Student:Student(char *id,char *name,int score)strcpy(Id,id);strcpy(Name,name);Score=score;Student:Student(Student &s)strcpy(Id,s.Id);strcpy(Name,s.Name);Score=s.Score;Student:Student()ostream& operator<<(ostream& out,Student& stu)out<<stu.Id <<'t'<<stu.Name <<'t'<<stu.Score <<

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論