assn7_soln - F3<<S2<<endl F1.close F2.close...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms .
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6.096 Lab 7 Solutions #include<iostream> #include<fstream> #include<conio.h> #include<math.h> #include<string.h> using namespace std; // Part 1 void CreateTextFile() { fstream F1,F2; F1.open("FILE1.TXT",ios::out); F2.open("FILE2.TXT",ios::out); F1<<"Hello"<<endl; F1<<"I"<<endl; F1<<"C++"<<endl; F2<<"World"<<endl; F2<<"Love"<<endl; F1.close(); F2.close(); } // Part 2 void MergeFiles() { fstream F1,F2,F3; F1.open("FILE1.TXT",ios::in); F2.open("FILE2.TXT",ios::in); F3.open("FILE3.TXT",ios::out); string S1,S2; while((F1>>S1)&&(F2>>S2)) { F3<<S1<<endl; F3<<S2<<endl; } while(F1>>S1) { F3<<S1<<endl; } while(F2>>S2)
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { F3<<S2<<endl; } F1.close(); F2.close(); F3.close(); } // Part 3 void AddtoFiles() { fstream F1; F1.open("FILE3.TXT",ios::app); F1<<"I Love 6.096 too!"<<endl; char str[80]; char ch; do { cout<<"Text?";cin.getline(str,80); F1<<str<<endl; cout<<"Continue?(Y/N)";cin>>ch; }while(ch!='N'); F1.close(); } // Part 4 void CountO() { fstream F1; char ch; int CountO=0; F1.open("FILE1.TXT",ios::in); while(!F1.eof()) { ch=F1.get(); if(ch=='O' || ch=='o') CountO++; } cout<<"This has :"<<CountO<<" O's "<<endl; F1.close(); } void CountLove() { fstream F1; char str[80]; int CountL=0; F1.open("FILE1.TXT",ios::in); while( F1>>str ) { if(strcmp(str,"Love")==0) CountL++; } cout<<"This has :"<<CountL<<" Loves "<<endl; F1.close(); }...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern