assn7_soln - {...

Info iconThis 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 .
Background image of page 1

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

View Full DocumentRight 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; { F3<<S1<<endl; F3<<S2<<endl; } while(F1>>S1) { F3<<S1<<endl; } while(F2>>S2)
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

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

Page1 / 3

assn7_soln - {...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online