08输入输出流解读
int x=10; fout.seekp( 20, ios::beg ); fout.write( (const char *)(x), sizeof(int) ); fin.seekg( 0, ios::beg ); fin.read( (char *)(x), sizeof(int) ); 二进制文件读写, 直接写二进制数据, 记事本看未必正确 //下面的程序从键盘输入几个学生的姓名的成绩, //并以二进制, 文件形式存起来 #include iostream #include fstream #include cstring using namespace std; class CStudent { public: char szName[20]; int nScore; }; int main() { CStudent s; ofstream OutFile( “c:\\tmp\\students.dat”, ios::out|ios::binary ); while( cin s.szName s.nScore ) { if( stricmp(s.szName, “exit” ) == 0) break; OutFile.write( (char *) s, sizeof(s) ); } OutFile.close(); return 0; } //名字为exit则结束 输入: Tom 60 Jack 80 Jane 40 exit 0 则形成的 students.dat 为 72字节 用 记事本打开, 呈现: Tom 烫烫烫烫烫烫烫烫Jack 烫烫烫烫烫烫烫蘌 Jane 烫烫烫烫烫烫烫? Note -- 文本文件/二进制文件打开文件的区别: ? 在Unix/Linux下, 二者一致, 没有区别; ? 在Windows下, 文本文件是以 “\r\n”作为换行符 ?读出时, 系统会将0x0d0a只读入0x0a ?写入时, 对于0x0a系统会自动写入0x0d //下面的程序将 students.dat 文件的内容读出并// 显示 #include iostream #include fstream using namespace std; class CStudent { public: char szName[20]; int nScore; }; void main() { CStudent s; ifstream inFile( students.dat, ios::in | ios::binary ); if( !inFile ) { cout error endl; return ; } while( inFile.read( (char* ) s, sizeof(s) ) ) { int nReadedBytes = inFile.gcount(); cout s.szName s.nScore endl; } inFile.close(); } 输出: Tom 60 Jack 80 Jane 40 //看刚才读了多少字节 // 下面的程序将 students.dat 文件的Jane的名// 字改成Mike #include iostream #include fstream using namespace std; class CStudent { public: char szName[20]; int nScore; }; void main() { CStudent s; fstream iofile( “c:\\tmp\\students.dat”, ios::in|ios::out|ios::binary); if ( !iofile ) { cout error ; } iofile.seekp( 2
您可能关注的文档
- 拓展部培训方案讲述.ppt
- 招投标流程及要点-上传版讲述.ppt
- 082709_合肥工业大学数电题集解读.doc
- 基层培训心得精要.pptx
- 招投标管理系统评标专家管理模块需求说明书讲述.doc
- 招投标评审案例讲述.doc
- 招投标管理学习讲述.pptx
- 08_AutoCAD在化工设备解读.ppt
- 招新发布会—部门介绍讲述.ppt
- 08_连续信号控制电路解读.ppt
- 2026及未来5年中国电磁铁行业市场发展监测及投资战略咨询报告.docx
- 2026及未来5年中国建筑工程机械行业市场发展监测及投资战略规划报告.docx
- 2026及未来5年中国硫化镍行业市场调研分析及投资战略规划报告.docx
- 2026及未来5年中国烷化剂市场运营态势及发展前景预测报告.docx
- 2026年涤纶细旦长丝项目可行性研究报告(市场数据调查、监测研究).docx
- 2026年弓型虫抗体试剂项目可行性研究报告(市场数据调查、监测研究).docx
- 2026年拖链系统用高柔性数据传输电缆项目可行性研究报告(市场数据调查、监测研究).docx
- 2025年中国水晶圆盘市场调查研究报告.docx
- 2025年中国长网双辊挤浆机市场调查研究报告.docx
- 2026年及未来5年内中国液体氩气行业投资前景及策略咨询研究报告.docx
最近下载
- 《影视视听语言》第四版 07189 第六、七、八、九章.pptx VIP
- 声带息肉手术患者护理查房.pptx VIP
- 《影视视听语言》第四版 07189第五章.pptx VIP
- 《影视视听语言》第四版 07189第三章.pptx VIP
- 《影视视听语言》第四版 07189 第一章课件.pptx VIP
- 高一地理自然地理环境的差异性.pptx VIP
- 《影视视听语言》第四版 07189 第二章.pptx VIP
- 2026年南宁职业技术学院单招职业适应性考试模拟测试卷附答案解析.docx VIP
- 高中地理 人教版 选修一《自然环境的整体性与差异性》自然环境的整体性 课件.pptx VIP
- 健康证考试题及答案.docx VIP
原创力文档

文档评论(0)