}
if(!outputfile) { }
while((ch = inputfile1.get()) != EOF)outputfile.put(ch); while((ch = inputfile2.get()) != EOF)outputfile.put(ch); cout<<\文件复制成功!\inputfile1.close(); inputfile2.close(); outputfile.close();
//关闭已打开的读文件 //关闭已打开的读文件 //关闭已打开的写文件
cout<<\打开文件失败!\outputfile1.close (); outputfile2.close (); return;
//关闭已打开的读文件
cout<<\打开文件失败!\inputfile1.close(); return;
ofstream outputfile(argv[3],ios::out|ios::trunc);//打开写文件
}
2. 编程序实现一个文本文件中所有的小写字母转为大写字母后打印出来。 #include
char ch;
ifstream inputfile(\打开读文件 if(!inputfile) {
cout<<\打开文件失败!\ return; }
while((ch = inputfile.get()) != EOF) {
if(ch>='a' && ch<='z')ch -= 32; //小写字母转为大写字母 cout< inputfile.close(); //关闭已打开的读文件 } 3.从键盘输入若干名学生的信息(姓名、学号和成绩),写入二进制文件,再从该文件中读出学生信息,输出在屏幕上。 #include \#include \#include \class Student { long no; char name[16]; int score; public: Student(){} Student(int n,char na[],int s)//构造函数 { no=n; strcpy(name,na); score = s; } void disp() { cout<<\ } }; void main() { ofstream outfile(\ if(!outfile) { cout<<\打开文件d:\\\\sdata.dat失败!\ return; } Student s1(2013001,\张三\创建第1个学生对象 Student s2(2013002,\王五\创建第2个学生对象 Student s3(2013003,\刘六\创建第3个学生对象 Student s4(2013004,\李四\创建第4个学生对象 Student s5(2013005,\刘七\创建第5个学生对象 outfile.write ((char *)&s1,sizeof(s1));//将5个学生对象分别写入文件 outfile.write ((char *)&s2,sizeof(s2)); outfile.write ((char *)&s3,sizeof(s3)); outfile.write ((char *)&s4,sizeof(s4)); outfile.write ((char *)&s5,sizeof(s5)); outfile.close(); //关闭文件 ifstream inputfile(\ if(!inputfile) { cout<<\打开文件d:\\\\sdata.dat失败!\ return; } Student t; inputfile.seekg(0L,ios::beg); inputfile.read ((char *)&t,sizeof(t)); while(!inputfile.eof ()) //A { t.disp (); inputfile.read ((char *)&t,sizeof(t)); } inputfile.close (); } 4. 编一程序,实现对学生成绩信息进行管理和维护,包括学生成绩的增加、修改、删除和查询、分数的统计等功能。其中学生信息包括姓名、学号、数学、政治和英语三门功课的成绩。 搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新初中教育面向对象程序设计(C++)自我测试练习参考答案 (5)全文阅读和word下载服务。
相关推荐: