实验七C++的输入输出流库.docVIP

  • 87
  • 0
  • 约3.62千字
  • 约 5页
  • 2017-03-28 发布于重庆
  • 举报
实验七C的输入输出流库

1从键盘输入一个字符串,并逐个将字符串的每个字符传送到磁盘文件中,字符串的结束标记为“!”。 #includeiostream #includefstream #includestring using namespace std; void main() { ofstream myf(e:\\aaa.txt); char txt[1000]; int i; cin.getline(txt,100); for(i=0;i100;i++) { if(txt[i]==!) break; myftxt[i]; } } . 输出十进制、八进制、十六进制显示的数据0~15。 #includeiostream #includeiomanip using namespace std; void main() { int i; int shuzu[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; coutDecimal:; for(i=0;i16;i++) { coutdecshuzu[i] ; } coutendl; coutOctal:; for(i=0;i16;i++) { coutoctshuzu[i] ; } coutendl; coutHexadecimal:; for(i=0;i16;i++) { couthexshuzu[i] ; } coutendl; } 3 类stu用来描述学生的姓名、学号、数学成绩、英语成绩,分别建立文本文件和二进制文件,将若干学生的信息保存在文件中,读出该文件的内容。 #include iostream #include fstream using namespace std; class student { public: char name[10]; int num; float math; float english; friend ostream operator (ostream out, student s); friend istream operator (istream in, student s); }; ostream operator (ostream out, student s) { outs.name s.num s.math s.english\n; return out; } istream operator (istream in, student s) { ins.names.nums.maths.english; return in; } void main( ) { ofstream ofile; ifstream ifile; ofile.open(e:\\d.txt); student s; int a; cout请输入需要输入信息的学生人数:; cina; coutendl; for(int i=1;i=a;i++) { cout请输入第i个学生的姓名,学号,数学成绩,英语成绩endl; cins; ofiles; } ofile.close( ); ifile.open(e:\\d.txt); cout\n读出文件内容endl; ifiles; while(!ifile.eof()) { couts; ifiles; } ifile.close( ); } 设计一个留言类,实现以下的功能。 (1) 程序第一次运行时,建立一个message.txt文本文件,并把用户输入的信息存入该文件。 (2) 以后每次运行时,都先读取该文件的内容并显示给用户,然后由用户输入新的信息,退出时将新的信息存入这个文档。文件的内容,既可以是最新的信息,也可以包括以前所有的信息,用户可自己选择。 #include iostream #include fstream using namespace std; class Message { public: char msg[20]; friend ostream operator (ostream out, Message s); friend istream operator (istream in, Message s); }; ostream o

文档评论(0)

1亿VIP精品文档

相关文档