9 Pages

4. File Streams

Course: ENGR 101, Fall 2010
School: Michigan
Rating:
 
 
 
 
 

Word Count: 851

Document Preview

Streams ENGR File 101, Lecture 4: 20 Sep 10 Announcements Project 2 is due Wednesday night at 9pm More on the project later in this lecture 1 Streams Sequences of data made available over time Input Stream Source of data, accessed incrementally In C++, accessed by extraction operator (>>) C++ standard input: cin Output Stream element by element Destination for data, presented...

Register Now

Unformatted Document Excerpt

Coursehero >> Michigan >> Michigan >> ENGR 101

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Streams ENGR File 101, Lecture 4: 20 Sep 10 Announcements Project 2 is due Wednesday night at 9pm More on the project later in this lecture 1 Streams Sequences of data made available over time Input Stream Source of data, accessed incrementally In C++, accessed by extraction operator (>>) C++ standard input: cin Output Stream element by element Destination for data, presented incrementally In C++, presented by insertion operator (<<) C++ standard input: cout Files Basic units of persistent information maintained by an operating system Organized in a file system, with file names and (typically) hierarchical directories Pathname: specifies sequence of contained directories, culminating in file name Examples: /afs/umich.edu/user/w/e/wellman/engr101/myprog.cpp ~/engr101/myprog.cpp 2 The fstream Library If we include the fstream library by typing #include <fstream> then we can define new streams that can get input from, and direct output to, files. fstream adds two new types: ifstream and ofstream. stand for input file stream and output file stream subtypes of istream and ostream, so they can be used just like cin and cout C++ Stream Types istream cin ifstream operations: >> ostream cout ofstream operations: << 3 Reading from Files To read from a file you first must declare a new ifstream and open it. Format: ifstream infile("thisfile.txt"); opens the file at pathname "thisfile.txt" declares and creates a new ifstream called infile stream named infile now eligible for extracting input Writing to Files To write to a file you first must declare a new ofstream and open it. Format: ofstream outfile("thatfile.txt"); opens the file at pathname "thatfile.txt" declares and creates a new ofstream called outfile stream named outfile now eligible for inserting output 4 File Stream Example istream x 25 ostream cin y 15 thisfile.txt cout ifstream infile 25 15 operations: >> ifstream infile("thisfile.txt"); int x, y; infile >> x >> y; ofstream outfile operations: thatfile.txt x = 25 y =15 << ofstream outfile("thatfile.txt"); outfile << "x =" << x << endl; outfile << "y =" << y << endl; Opening and Closing Files Recall that the statement ifstream infile("thisfile.txt"); does two things: It declares a new input file stream called infile It opens the file thisfile.txt and associates it with this stream This can also be performed in two separate steps ifstream infile; infile.open("thisfile.txt"); 5 Opening and Closing Files When we are done using a file it is good practice to close the file ifstream infile; infile.open("thisfile.txt"); infile >> x >> y; infile.close(); open and close are operations defined on ofstream and ifstream objects The fstream Library istream cin ostream cout ifstream ofstream operations: open, close operations: open, close operations: >> operations: << 6 What is in outfile.txt after this Program Executes? #include <iostream> #include namespace <fstream> using std; A Alpha int main( ) { ofstream outfile("outfile.txt"); B cerr << "Alpha" << endl; cout << "Beta" << endl; C outfile << "Gamma" << endl; outfile.close(); return 0; D } Beta Gamma Alpha Beta Gamma What is in outfile.txt after the second program executes? #include <iostream> #include <fstream> using namespace std; int main( ) { A int x=4; ofstream outfile("outfile.txt"); B outfile << x << x+1 << x+3; outfile << x-1 << endl; outfile.close(); C return 0; } 4573 457 3 4587 D 458 7 7 What is in outfile.txt after the third program executes? #include <iostream> #include <fstream> using namespace std; void printproc(ofstream & zfile, int a){ zfile << a << a << endl; return; } A int main( ){ ofstream outfile("outfile.txt"); printproc(outfile, 7590); outfile.close(); return 0; } B 7590 C 75907590 D 7590 7590 Tracing Program 3 #include <iostream> #include <fstream> using namespace std; void printproc(ofstream & zfile, int a){ zfile << a << a << endl; return; a zfile } 7590 outfile.txt 75907590 int main( ){ ofstream outfile("outfile.txt"); printproc(outfile, 7590); outfile.close(); return 0; outfile } 8 What is output to the screen after the fourth program executes? int main( ){ int a, b, c, d, e, f; ofstream outfile("outfile.txt"); permute(outfile, 1, 2, 3); outfile.close(); ifstream infile("outfile.txt"); infile >> a >> b >> c >> d >> e >> f; infile.close(); cout << f << a << endl; cout << e << b << endl; cout << d << c << endl; return 0; } A 21 32 13 B 31 23 12 void permute(ofstream & fout, int a, int b, int c) { fout << a << b << c << endl; fout << a << c << b << endl; fout << b << a << c << endl; fout << b << c << a << endl; fout << c << a << b << endl; fout << c << b << a << endl; return; } C 312132 231213 312132 D 321123 312132 231213 Tracing Program 4 int main( ){ int a, b, c, d, e, f; ofstream outfile("outfile.txt"); permute(outfile, 1, 2, 3); outfile.close(); ifstream infile("outfile.txt"); infile >> a >> b >> c >> d >> e >> f; infile.close(); cout << f << a << endl; cout << e << b << endl; cout << d << c << endl; return 0; } void permute(ofstream & fout, int a, int b, int c) { fout << a << b << c << endl; fout << a << c << b << endl; fout << b << a << c << endl; fout << b << c << a << endl; fout << c << a << b << endl; fout << c << b << a << endl; return; } 123 132 213 231 312 321 a >> b >> c >> d >> e >> f >> 321123 312132 231213 9
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Michigan - ENGR - 101
Iteration and ConditionalsENGR 101, Lecture 5: 22 Sep 10AnnouncementsProject 2 due tonight (Wed 9pm)Exam #1 Tue 5 OctM. Wellmanif you have a potential conflict, contact Nader Jawad(njawad@umich.edu)Classroom evacuation routes1Fundamental Algorit
Michigan - ENGR - 101
ENGR 101, Sec 100, Fall 10SelectionENGR 101, Lecture 6: 27 Sep 10AnnouncementsProject 3 due Wed 9PM (29 Sep)Exam #1 Tue 5 OctM. Wellmanif you have a potential conflict, contact Nader Jawad(njawad@umich.edu) ASAP1ENGR 101, Sec 100, Fall 10Fundam
Michigan - ENGR - 101
ENGR 101 Sec. 100 Fall 10Predicates and LoopsENGR 101, Lecture 7: 29 Sep 10AnnouncementsProject 3 due Wed 9PM (29 Sep)Exam #1 Tue 5 OctProf. M. Wellmansample exams available this weekendtake practice gateway exam for participation creditreview se
Michigan - ENGR - 101
ENGR 101, Sec 100, Fall 10Counting LoopsENGR 101, Lecture 8: 6 Oct 10AnnouncementsM. WellmanProject 4 out, due Wed 13 Oct 9pmExam #1 grades1ENGR 101, Sec 100, Fall 10Finite SumsSuppose we want to compute a summationSum(m) =Sum(4) =Summation F
Michigan - ENGR - 101
ENGR 101Data TypesENGR 101, Lecture 9: 11 Oct 10Today's QuestionHow high can you count on the fingers of one hand?1ENGR 101AnnouncementsProject 4 due Wed 9pmHow is your progress on Project 4?Have not started yetB. Started, not very far yetC. H
Michigan - ENGR - 101
ENGR 101Names: Variable, Scope, LifetimeENGR 101, Lecture 10: 13 Oct 10AnnouncementsProject 4 due tonight, 9pmProject 5 out soon, 2-week (almost) assignment1ENGR 101NamesProgramming entails introduction and management ofnamesfor procedure: func
Michigan - ENGR - 101
ENGR 101Classes and Generic RoutinesENGR 101, Lecture 11: 20 Oct 10AnnouncementsProject 5 due Wed 27 Oct (9pm)Exam 2 on Wed 3 Nov (6pm)contact Nader Jawad (njawad@umich.edu) about conflictsMid-term course evaluationsthanks for constructive feedbac
Michigan - ENGR - 101
ENGR 101String Class, Array SemanticsENGR 101, Lecture 12: 25 Oct 10AnnouncementsProject 5 due Wed 27 Oct (9pm)Exam 2 on Wed 3 Nov (6pm)contact Nader Jawad (njawad@umich.edu) about conflictsHow is your progress on Project 5?A.B.C .D.E.Have no
Michigan - ENGR - 101
ENGR 101, Section 100VectorsENGR 101, Lecture 13: 27 Oct 10AnnouncementsProject 5 due tonight (9pm)Exam 2 on Wed 3 Nov (6pm)M. Wellmancontact Nader Jawad (njawad@umich.edu) about conflictsGateway exam coming (probably in lab)1ENGR 101, Section 1
Michigan - ENGR - 101
ENGR 101, Section 100, Fall 10Recursion and SortingENGR 101, Lecture 14: 1 Nov 10AnnouncementsExam 2 on Wed 3 Nov (6pm)Project 6 due Wed 10 Nov (9pm)M. Wellmanwatch for room announcementsOpen book/notes, same style/rules as Exam 1Samples posted,
Michigan - ENGR - 101
ENGR 101, Section 100Matrix CalculationsENGR 101, Lecture 15: 8 Nov 10Background SurveyA.B.C .D.M. WellmanHow much do you already know about matrix algebra?Good facility with linear algebraSome basic matrix operations (e.g., multiplication)Hav
Michigan - ENGR - 101
ENGR 101, Section 100Gaussian Elimination, andIntro to MATLABENGR 101, Lecture 16: 10 Nov 10AnnouncementsProject 6 due tonight at 9pm1ENGR 101, Section 100Q1: Triangular?Q2: How to solve?Convert to a Matrix-1 1/20 -1/2000010000 -1/20 1/2
Michigan - ENGR - 101
ENGR 101, Section 100, Fall 09Matrices in MATLABENGR 101, Lecture 17: 15 Nov 10AnnouncementsLook for Project 7 this week (due Wed 1 Dec)First MATLAB assignment1ENGR 101, Section 100, Fall 09MATLAB Data Elements (review)all data is some kind of ar
Michigan - ENGR - 101
ENGR 101, Section 100, Fall 10Scripts, Functions, Input/OutputENGR 101, Lecture 18: 17 Nov 10AnnouncementsProject 7 posted (due Wed 1 Dec)My office hoursM. Wellmanmay require some constructs from next week, but you haveenough Matlab to get a good
Michigan - ENGR - 101
ENGR 101 Section 100, Fall 10MATLAB Programming (Part I)ENGR 101, Lecture 19: 22 Nov 10AnnouncementsProject 7 due Wed 1 DecM. WellmanTry to get done early, to avoid MATLAB license contention1ENGR 101 Section 100, Fall 10Save/LoadMATLAB provides
Michigan - ENGR - 101
ENGR 101 Section 100MATLAB Programming (Part II)ENGR 101, Lecture 20: 24 Nov 10AnnouncementsProject 7 due Wed 1 DecM. WellmanTry to get done early, to avoid MATLAB license contention1ENGR 101 Section 100Relational Operators on ArraysRelational o
Michigan - ENGR - 101
ENGR 101, Section 100, Fall 10Subarrays and VectorizationENGR 101, Lecture 21: 29 Nov 10AnnouncementsProject 7 due Wed 1 DecM. WellmanTry to get done early, to avoid MATLAB license contentionNote on conversion to doublesWatch for Project 8 soon1
Michigan - ENGR - 101
ENGR 101, Section 100, Fall 10Visualizing DataENGR 101, Lecture 22: 1 Dec 10AnnouncementsProject 7 due tonight: Wed 1 DecProject 8 out tonight, due Mon 13 DecM. WellmanNo extensions possibleDealing with MATLAB license contention1ENGR 101, Sectio
Michigan - ENGR - 101
ENGR 101, Section 100, Fall 10Fractals and the Mandelbrot SetENGR 101, Lecture 23: 8 Dec 10AnnouncementsProject 8 due Mon 13 DecExam 3 Thu 16 Dec, 810amM. Wellmanopen book &amp; notesno computation or communication devicessimilar format to previous e
Michigan - ENGR - 101
10/3/10 Review: C+ FundamentalsENGR 101: 4 Oct 10Where Weve BeenAlgorithms (what its all about)A high-level programming language: C+Data types and declarationExpression statementsOperators: Arithmetic, Assignment, Input/Output,Relational, Logica
Michigan - ENGR - 101
ENGR 101, Sec,on 100, Fall 10 11/2/10 Review: Programming and C+ENGR 101: 3 Nov 10Where Weve Been Since Exam 1Counting Loopscounting patterns, for, nested loopsData TypesNamesvariables, constants, scope, lifetimeClassesdata types, encodi
Michigan - ENGR - 101
ENGR 101, Sec,on 100, Fall 10 12/12/10 Review: Programming in MATLABENGR 101: 13 Dec 10AnnouncementsProject 8 due tonight 9pmAll regrade requests must be in before Final examExam 3 Thu 16 Dec, 810amTo be graded asapopen book &amp; notesno com
Michigan - ENGR - 101
Exam 2 - AnswersFall 2009 SemesterEngr101: Introduction to Computers and ProgrammingSection 100 / 200You are allowed to use the books, your notes, and a calculator.You cannot use devices that allow the passing of information between others.You canno
Michigan - ENGR - 101
Exam 2Fall 2009Engr101: Introduction to Computers and ProgrammingSection 100 / 200You are allowed to use the books and your notes.You cannot use electronic devices including, but not limited to, calculators, laptops, and cell phones.You cannot acces
Michigan - ENGR - 101
Exam 3 AnswersFall 2009Engr101: Introduction to Computers and ProgrammingSection 100 / 200You are allowed to use the books and your notes.You cannot use electronic devices including, but not limited to, calculators, laptops, and cell phones.You cann
Michigan - ENGR - 101
Exam 3Fall 2009Engr101: Introduction to Computers and ProgrammingSection 100 / 200You are allowed to use the books and your notes.You cannot use electronic devices including, but not limited to, calculators, laptops, and cell phones.You cannot acces
Michigan - ENGR - 101
Exam 2 - AnswersWinter 2010Engr101: Introduction to Computers and ProgrammingSection 200You are allowed to use the books and your notes.You may not use electronic devices: including, but not limited to, calculators, laptops, and cell phones.You may
Michigan - ENGR - 101
Exam 2Winter 2010Engr101: Introduction to Computers and ProgrammingSection 200You are allowed to use the books and your notes.You may not use electronic devices: including, but not limited to, calculators, laptops, and cell phones.You may not access
Michigan - HIST - 303
Notes on the Balinese CockfightChapter I s/Deep Play:N otes o n t heB alinese CockfightT he RaidEarly in April o f 1958, my wife and I arrived, malarial and diffident,ina Balinese village we intended, as ianthropologists, to study. A smallplace, ab
Michigan - HIST - 303
174&quot;,\A n Essay on Sport and Violencemorals stood on one side, morals without manners o n t he other. Earlyin the eighteenth century the two traditions began to move closer toeach other. The attempt made by Addison and Steele to reconcilemorals and
Michigan - HIST - 303
IF;\':~I NTRODUCTION&gt;':l~- - - &lt;&quot;~:;-'.'itT he M exican radicals h ad a lready received enthusiastic suppOrt fro.:~G iuseppe G aribaldi a nd o ther r evolutionaries who h ad b een t he heroes : .;~]t he 1 848 rebellioris against authority i n E ur
Michigan - HIST - 303
I.T he body o f the condemnedO n 2 March 1757 Damiens the regicide was condemned 'to makethe amende honorable before the main door o f the Church o f Paris',where he was to be 'taken and conveyed in a cart, wearing nothingbut a shirt, holding a torch
Michigan - HIST - 303
Sport as a Sociological Problem3''T he G enesis o f S port as aSociological P roblemN orbert EliasIMany types o f sports which today are played in a more o r less identicalmanner all over the world originated in England. 1 They spread fromthere
Michigan - HIST - 303
Michigan - HIST - 303
1P UBLICISTS, P ROPAGANDISTS A NDP ROSELYTIZERSIdeals o f Empirefor Public SchoolboysO nce the Empire was established, the public schools sustained it.In the words o f G. Kendall, onetime headmaster o f U niversityCollege School, ' The public school
Michigan - HIST - 303
Tennyson, Charles, Sir, THEY TAUGHT THE WORLD TO PLAY , Victorian Studies, 2:3(1959:Mar.) p.211Tennyson, Charles, Sir, THEY TAUGHT THE WORLD TO PLAY , Victorian Studies, 2:3(1959:Mar.) p.211Tennyson, Charles, Sir, THEY TAUGHT THE WORLD TO PLAY , Victo
Michigan - HIST - 303
C HRIST A ND T HE I MPERIAL G AMES F IELDS/./\tl'1:91\ / , \ &quot;. ~ ~y\\ ,J/~'~.') \l.~r &quot;)l.,~~V\&quot;&gt; .'ic;r7C HRIST A ND T HE I MPERIALG AMES F IELDSEvangelical Athletes o j the Empire_ 1iOi&gt;C hristianity, B uddhism a nd I slam have i n
Michigan - HIST - 303
, ;~.~Chapter 4The Recreational Experienceso f Early American WomenNancy L. StrunaI cannot say that I think you very generous to the ladies, f or whilstyou are proclaiming peace and good will to men, emancipating allnations, you insist upon retain
Michigan - HIST - 303
Chapter 9V ARIETIES O FF OOTBALLj'Of all the team sports t hat became organized in the nineteenth century,football was t he m ost i nternational i n a ppeal a nd diverse i n form. Nourishedin England's prestigious &quot; public&quot; (private) schools, the ol
Michigan - HIST - 303
PRAYING AND PLAYING IN THE YMCA3PRAYING ANOPlAYING IN THE YMCAThe ideological trailblazers of muscular Christianity were not run-of-themill folk. Most came from highly privileged backgrounds, were trained inthe classics at private boys' schools, and r
Michigan - HIST - 303
rr':u,;I'iC HAPTER V IIII'T HE F OREIGN S PREADT HE G AME C ROSSES T HE P ACIFICA n E arly B asketball G ame i n J apanA SKETBALL was accepted in m any f oreigncountries soon 'after the game was first playedthe U nited S tates. , It was early
Michigan - HIST - 303
The Wistful Camel and the Eye o f the Needlecricket field. 32W hat was left unsaid in such compliments had already been ~aid bythe redoubtable Sir P.M. Mehta (1845-1915) at a farewell dinner inhonour o f the team before its departure from India:The
Michigan - HIST - 303
1Remaking Manhood through Race a nd &quot;Civilization&quot;A t2:30 P.M. o nJuly4, 1910, i n Reno, Nevada, as the b and played &quot;All CoonsLook Alike to Me,&quot; Jack J ohnson climbed into the ring to defend his titleagainst J im Jeffries. Johnson was the first Afric
Michigan - HIST - 303
The Rise o f International Sports Organizations2T he Rise o f InternationalSports OrganizationsAs the nineteenth century's b urst o f nationalism a nd imperialism p ushedt he w orld toward a n i ntegrated system of nation-states, i t also spawnedn e
Michigan - HIST - 303
C hapter 3t HE N AZI OLYMPICS O F 1936A rnd KrugerC ommonlyreferred t o as the ' Nazi O lympics' ( Mandell, 1971; K ruger &amp;M urray, 2003), t he O lympic G ames o f 1936 c hanged t he O lympic movementin m agnitude a nd p roportion. As the focal p oin
Michigan - HIST - 303
. Children into Soldiers: Sport and Fascist I taly8-Children i nto S oldiers:S port a nd F ascist I talyR OBERTA V ESCOVIFascism from the beginning had a primary aim, that o f m oulding thecountry's youth according to Fascist ideals. U nder t he inf
Michigan - HIST - 303
Michigan - HIST - 303
R ichard G iulianotti a nd R oland R obertsonSwyngedouw, E. (1992) 'The Mammon quest: &quot;glocalization&quot;, interspatial competition and themonetary order: the construction o f new scales', in M. Dunford and G. Kafkalis (eds) Citiesand regions in the new Eu
Michigan - HIST - 303
Michigan - HIST - 303
IIIII2Not playing around: global capitalism,modern sport and consumer cultureB ARRY SMARTThroughout the twentieth century leading sporting figures, chairmen o f economiccorporations with direct and indirect interests in sport, think tanks, and s
Michigan - HIST - 303
Televised Sport in a Global Consumer Age11Televised Sport in a Global Consumer AgeM ICHAEL S ILKThere is all around us today a kind o f fantastic conspicuousness o f consumption andabundance, constituted by the multiplication o f objects, services an
Michigan - HIST - 303
C hapter 4Circuits ofPromotion: Media,Marketing and theGlobalization ofSportDavid WtlitstmT hiss ets o ut t osomeaspocts o f t he production andco:ru;utml&gt;li&lt;m o f s pon in the late twentiefh cellll!l''. !11 tile 1990s, fhe Norfh Amerk1111base
Michigan - HIST - 303
Chapter Ten/The Silence o f the RamsH ow St. Louis School Children Subsidize/ the Super Bowl ChampsGeorge LipsitzWhen the St. Louis Rams defeated the Tennessee Titans o n January 23,2000, to win the Super- Bowl, the team's players, coaches, and man
Michigan - HIST - 303
1'14I NTRODUCTIONtry ~l~bbers to mean bewildered/ vacuous/ slightly hedonistic a nd mamp olitically i ncorrect p eople . b . h 11m othlym n g t y c o o red c lothes T he1hf ounded a nd n urtured t he 1 b b.p eop e w oc u s e tween 1 880 a nd 1
Michigan - HIST - 303
Michigan - HIST - 303
Chapter 4THE COLD WAR GAMESCesar R. Torres a nd M ark DyresonIn 1945, the 'new Germany', so cleverly advertised by A dolf H itler and theNazis a t t he 1936 Olympic G ames, laid in ruins. Hitler's scheme for theconstruction o f a colossal 450,000 s t
Michigan - HIST - 303
Michael JordanNewGlobal Capitalisma nd theA LSO BY W ALTER L AFEBERThe Clash: U.S. -Japanese Relations throughout HistoryThe American Age: U.S. Foreign Policy a t Home andAbroad since 1750Inevitable Revolutions: The United States in Central Americ
Michigan - HIST - 303
r28Commercialisation o f Sport104. A.M. Feder, ' &quot;A Radiant Smile from the Lovely Lady&quot;: Overdetermined Femininity on&quot;Ladies&quot; Figure Skating', in C. Baughman (ed.), Women on Ice: Feminist Essays on the TanyaHarding/Nancy Kerrigan Spectacle (New York:
Michigan - HIST - 303
Diaz &quot;Fight Boys&quot; (2002), p1/14Diaz &quot;Fight Boys&quot; (2002), p2/14Diaz &quot;Fight Boys&quot; (2002), p3/14Diaz &quot;Fight Boys&quot; (2002), p4/14Diaz &quot;Fight Boys&quot; (2002), p5/14Diaz &quot;Fight Boys&quot; (2002), p6/14Diaz &quot;Fight Boys&quot; (2002), p7/14Diaz &quot;Fight Boys&quot; (2002), p8/14