FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC....

12
FILE IO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC

Transcript of FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC....

Page 1: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

FILE IO AND MIDTERM REVIEWProblem Solving with Computers-I

7

Freq AC

Page 2: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

Announcements• Midterm next Thursday (Oct 24)

tHAre

Page 3: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

I/O in programsDifferent ways of reading data into programs• cin• Command line arguments (int main(int argc, char* argv[])• Read from file

Ways to output data• Standard output: cout• Standard error: cerr• Write to file

R

O

Page 4: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

Where are files stored?A. In main memoryB. In secondary memoryC. On the processorD. In C++ programsE. None of the above

Freq Al

O

r0

OfSIM secondaryMeme

p l

Page 5: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

Iit

Page 6: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

Writing to files#include <fstream>ofstream ofs; // Create a ifstream objectofs.open(“animals.txt”); //Open a file to write toofs<<“Duck\n”<<“Cat\n”<<“Cow\n”;

Page 7: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

Reading from files• Open a file• If open fails, exit• In a loop

• Read a line • If you reach the end of file, break• Else process the line that was read

• Close the file

Page 8: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

Reading from files#include <fstream>ifstream ifs; // Create a ifstream objectifs.open(“numbers.txt”); //Open a file to readif(!ifs){

// open failed}getline(ifs, line); // read a line from the file into a

// string line.// If you attempt to read past the end// of file, ifs change to false

// If the file was empty, ifs will be false at this pointifs.close()

Page 9: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

FILE IO: Which of the following is correct?while(1){

getline(ifs, line);if (!ifs)

break;cout<<line<<endl;

}

A.

B.

C.

D.

Both A and B are correct

while(ifs){getline(ifs, line);cout<<line<<endl;

}

Neither is correct

0

Page 10: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

BIG IDEA: Bits can represent anything!!

Characters‘a’

‘b’

‘c’

‘d’

‘e’N bits can represent at most 2N things

Page 11: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

What is the minimum number of bits required to represent all the letters in the English alphabet in lower case?

A.3B.4C.5D.6E.26

Page 12: FILEIO AND MIDTERM REVIEW · FILEIO AND MIDTERM REVIEW Problem Solving with Computers-I 7 Freq AC. Announcements •Midterm next Thursday (Oct 24) tH Are. I/O in programs ... In C++

Next time• Arrays