fileio and midterm review · fileio and midterm review problem solving with computers-i 7 freq ac....
TRANSCRIPT
![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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/1.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/2.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/3.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/4.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/5.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/6.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/7.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/8.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/9.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/10.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/11.jpg)
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++](https://reader033.vdocuments.net/reader033/viewer/2022041704/5e4389ef712c6716e5528755/html5/thumbnails/12.jpg)
Next time• Arrays