Skip to content
Topos Uranos

Summary

This lesson opens the seventh unit of the course, devoted to text and files. It explains what the unit is for: to teach how to treat text as data, with its real encoding, and how to make a program read and write files without breaking in the face of malformed input; it presents its four lessons and the thread that joins them; it recalls what the unit takes for granted, above all that a character is, for the machine, a number, and the vectors, records and functions of units 5 and 6; it formulates the problem the unit solves, that of a program that looks correct but loses data without warning, stops at the first unexpected line and mismeasures names with accented letters; it anticipates that a letter does not always take one byte; and it shows where it leads, in unit 8 and in the course Programming in C++ II.

Learning objectives

  1. Describe the sequence of contents of the unit and explain what each lesson contributes to the next.
  2. Recall the representation of characters as numeric codes (lesson 2.4) and write a code in binary and in hexadecimal.
  3. Anticipate, with an example, that a letter is not always a byte, and that therefore counting bytes is not counting letters.
  4. Recognize what one is expected to be able to do at the end of the unit, and distinguish it from what will be learned in the next unit and in Programming in C++ II.

Entry assessment

Before working through the unit, it is worth measuring the starting point. This assessment is a self-assessment: it is not graded, and its only purpose is to guide your study. The first six questions check what the unit takes for granted: the code of a character, writing a number in binary and in hexadecimal, a bit mask, traversing a vector while filtering its elements, what happens when a read receives letters instead of a number, and the definition of a record. The next four touch, in the order of the lessons, on the central ideas of the unit: what a search that finds nothing returns, the size of a word with an accented letter, the empty line that appears when two ways of reading are mixed, and the file that could not be opened. No one is obliged to get these four right yet, and in some of them “I don’t know yet” is an honest answer: each answer, right or not, explains the point and names the lesson that deals with it. The same skills are measured again at the end, in the synthesis lesson.

Entry assessment

Before starting the unit, measure your starting point. It is a short self-assessment: it is not graded and its result is stored only in your browser.

  1. Question 1From earlier unitsWhat does std::println("{:d} {:d}", 'A', 'a'); write?

  2. Question 2From earlier unitsHow is 233 written in binary and in hexadecimal?

  3. Question 3From earlier unitsWhat is the value of 0xB1 & 0xC0?

  4. Question 4From earlier unitsWhat does std::vector<int> v{4, 7, 10, 3}; std::vector<int> w{}; for (const int x : v) { if (x % 2 == 0) { w.push_back(x); } } std::println("{}", w); write?

  5. Question 5From earlier unitsAfter int n{-1}; std::cin >> n;, the user types abc. What is the value of n, and what happens to the subsequent reads?

  6. Question 6From earlier unitsWhich of the following definitions correctly declares a record with a title and a year?

  7. Question 7If s is std::string s{"casa azul"};, what does s.find("x") return?

  8. Question 8What does std::println("{}", std::string{"año"}.size()); write in a program saved in UTF-8, like those of this course?

  9. Question 9A program executes int age{}; std::string name{}; std::cin >> age; std::getline(std::cin, name); and the user types 36, presses Enter, types Ana and presses Enter. What is left in name?

  10. Question 10A program executes std::ifstream in{"data.txt"}; int x{-1}; in >> x;, but data.txt does not exist in the folder from which it is run. What happens?