C实用教程[郑阿奇主编]14

上传人:宝路 文档编号:48103205 上传时间:2018-07-09 格式:PPT 页数:39 大小:316.07KB
返回 下载 相关 举报
C实用教程[郑阿奇主编]14_第1页
第1页 / 共39页
C实用教程[郑阿奇主编]14_第2页
第2页 / 共39页
C实用教程[郑阿奇主编]14_第3页
第3页 / 共39页
C实用教程[郑阿奇主编]14_第4页
第4页 / 共39页
C实用教程[郑阿奇主编]14_第5页
第5页 / 共39页
点击查看更多>>
资源描述

《C实用教程[郑阿奇主编]14》由会员分享,可在线阅读,更多相关《C实用教程[郑阿奇主编]14(39页珍藏版)》请在金锄头文库上搜索。

1、第14章 输入/输出流14.1 概述14.1.1 流和流类n在C+中,输入/输出操作是由“流”来处理的。流是 C+的一个核心概念,数据从一个位置到另一个位置 的流动抽象为流。 14.1.2 标准流对象nC+提供了4个预定义的标准流对象:cin、 cout、cerr和clog,它们都是std名称空间的流 对象。其中,cin是istream类的对象,用来处 理标准输入,即键盘输入;cout是ostream类 的对象,用来处理标准输出,即屏幕输出; cerr和clog都是ostream类的对象,用来处理标 准出错信息,并将信息显示在屏幕上 14.1.3 提取和插入运算符重载#include usin

2、g namespace std; class CStudent; ostream class CStudent public: friend ostream private: charstrName10;/ 姓名 charstrID10;/ 学号 floatfScore3; / 三门成绩 ;ostream coutstu.strID; coutstu.fScore0stu.fScore1stu.fScore2; return is;/ 返回is的目的是允许连续多个操作存在 int main() CStudent one; cinone; cout #include / 必须要有此头文件包含 u

3、sing namespace std; int main() intnNum = 1234; double fNum = 12.3456; cout #include using namespace std; int main() double d = 13.0/17.0; cout #include using namespace std; int main() intnNum = 1234; cout using namespace std; int main() int i, s; char buf80; couti; s = cin.rdstate(); couti; s = cin.

4、rdstate(); return 0; 14.3 使用输入/输出成员函数n如果想要使输入/输出控制更为细致,如希望 把输入的空格作为一个字符而不是分隔符,就 需要使用istream类和ostream类中的相关成员 函数。14.3.1 输入操作的成员函数n(1)使用get和getline函数用于输入字符或字符串的 成员函数get原型如下: int get(); istream istreamn第一种形式是从输入流中提取一个字符,并转换成整 型数值。第二种形式是从输入流中提取字符到rch中 。第三种形式是从输入流中提取一个字符串并由pch 返回 例Ex_GetAndGetLine get和get

5、line的使用示例。 #include using namespace std; int main() char s180, s280, s380; coutnusing namespace std;nint main()nnchar data80;ncout #include / 文件操作必须的头文件 using namespace std; int main() fstream file1;/ 定义一个fstream类的对象用于读 file1.open(“Ex_DataFile.txt“, ios:in); if (!file1) cout #include / 文件操作的头文件 usin

6、g namespace std; int main() char ch, str=“ABCDEFGHIJK“; ofstream outfile(“letter.dat“);/ 用构造 函数打开文件letter.dat if (!outfile) cout #include / 文件操 作的头文件 using namespace std; int main() char chs, che, str=“ABCDEFGHIJK“; fstream iofile(“letter.dat“,ios:in | ios:out );/ 用构造函数打开letter.dat用于读/ 写 if (!iofile

7、) cout #include #include #include #include using namespace std; class CStudent; ostreamclass CStudent public: CStudent() CStudent(char* name, char* id, float s1, float s2, float s3); void print( int n = -1); char* GetName(); friend ostream private: charstrName20; / 姓名 charstrID10; / 学号 floatfScore3;

8、 / 三门课成绩 ; CStudent:CStudent(char* name, char* id, float s1, float s2, float s3) strncpy(strName, name, 20);strncpy(strID, id, 10); fScore0 = s1;fScore1 = s2;fScore2 = s3; void CStudent:print( int n )/ n为序号 ,0) cout0) cout ( istream char id10; is.read(name, 20); is.read(id, 10); is.read(char*)stu.fS

9、core, sizeof(stu.fScore) ); strncpy(stu.strName, name, 20); strncpy(stu.strID, id, 10); return is; class CStuFile public: CStuFile(char* filename); CStuFile(); voidadd(CStudent stu);/ 添加记录 intseek(char* name, CStudent / 按姓名查找, 返回记录号, -1表示没有找到 voidlist();/ 列表显示 private: fstream thefile;/ 文件流 ; CStuFi

10、le:CStuFile(char* filename) thefile.open( filename, ios:in | ios:out | ios:trunc); if (!thefile) coutstu) if (strncmp(name, stu.GetName(), strlen(name) = 0) nRec = i;break; i+; return nRec; void CStuFile:list() / 列表显示 CStudent stu; / 将文件指针定位到文件头,然后显示 thefile.clear();/ 清除内部标志 thefile.seekg( 0 ); int

11、i = 0; while (thefilestu ) stu.print( i + 1 );i+; int main() CStuFile theFile( “student.dat“ ); CStudent stu1(“MaWenTao“,“99001“,88, 90, 75.5); CStudent stu2(“LiMing“,“99002“,92, 80, 81.5); CStudent stu3(“WangFang“,“99003“,89, 70, 78); CStudent stu4(“YangYang“,“99004“,90, 80, 90); CStudent stu5(“DingNing“,“99005“,80, 78, 85); theFile.add( stu1 );theFile.add( stu2 ); theFile.add( stu3 );theFile.add( stu4 ); theFile.add( stu5 ); theFile.list(); CStudent stu; int nRec = theFile.seek( “LiMing“, stu ); if (nRec=0) cout“找到的结果为:“endl;stu.print( nRec + 1 ); else cout“没有找到!“endl; return 0; 程序运行结果如下:

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 教学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号