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
- Describe the sequence of contents of the unit and explain what each lesson contributes to the next.
- Recall the representation of characters as numeric codes (lesson 2.4) and write a code in binary and in hexadecimal.
- Anticipate, with an example, that a letter is not always a byte, and that therefore counting bytes is not counting letters.
- 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.
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.
Question 1From earlier unitsWhat does
std::println("{:d} {:d}", 'A', 'a');write?It believes that lowercase letters immediately follow uppercase ones; between them there are 32 codes, with some symbols in the middle.It ignores that{:d}asks for the code; without it,std::printlnwould write the letter.It reverses the order: in ASCII, uppercase letters have smaller codes than lowercase ones.Acharstores a number, the code of the character, and{:d}asks for that number to be written rather than the letter. In ASCII, uppercase letters start at 65 and lowercase letters at 97: the distance is 32, a single bit. The lesson Text is not ASCII starts from here to explain what happens with the letters that are not in ASCII.Question 2From earlier unitsHow is 233 written in binary and in hexadecimal?
It groups the four low bits wrongly:1001is 9, not 1.It reads the bits backwards, from the least significant to the most significant.It confuses hexadecimal notation with decimal notation.. Grouped in fours from the right, the bits1110and1001are 14 and 9, that is,eand9. It is the code of the “é”, and the unit will show that in a present-day text file that code is not stored as a single bytee9, but as two.Question 3From earlier unitsWhat is the value of
0xB1 & 0xC0?It confuses&with|: the bitwise OR turns the high bits on instead of selecting them.It believes that the mask changes nothing; it clears the six low bits.It confuses&with^, the exclusive OR.0xB1is10110001and0xC0is11000000: the bitwise AND keeps only the two high bits of0xB1, which are10. That mask is the one the lesson Text is not ASCII uses to recognize the bytes that continue a letter.Review: Bits and the math library
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?It inverts the condition:x % 2 == 0is true for the even numbers.It believes thatpush_backcopies the entire vector, or that the filter is applied tovandvis written.It believes thatpush_backadds at the beginning; it adds at the end.The loop traversesvand copies intowonly the even numbers, in the order in which they appear;vdoes not change. The unit uses the same scheme with text: traversing the bytes of a string and keeping some of them, or traversing the lines of a file and keeping the valid ones.Review: Vectors
Question 5From earlier unitsAfter
int n{-1}; std::cin >> n;, the user typesabc. What is the value ofn, and what happens to the subsequent reads?It believes that the failed read leaves no trace; it leaves one in the variable and in the state of the stream.It assumes that C++ warns about invalid input; the program continues as if the read had gone well.It confuses reading an integer with reading a character.The read skips the whitespace, finds a letter that cannot begin a number and fails: the standard specifies that the variable receives 0, that the letter remains in the stream and that the stream enters an error state, with no message at all. The lesson Reading well teaches how to distinguish this failure from the end of the data and how to recover from it.Review: Input, output and errors
Question 6From earlier unitsWhich of the following definitions correctly declares a record with a title and a year?
It forgets the semicolon after the closing brace, the first mistake of anyone who writes records; the compiler points it out precisely.It separates the fields with commas, like the elements of a list; each field is a declaration and ends with a semicolon.It confuses a record, with named fields of different types, with a vector, whose elements are all of the same type.Each field is declared like a variable, with its type, its name and its initial value in braces, and the definition ends with a semicolon after the closing brace. The lesson Text files fills vectors of records like this one with the lines of a file.Review: Records
Question 7If
sisstd::string s{"casa azul"};, what doess.find("x")return?It is the convention of other languages;std::size_thas no sign, and a-1converted to it is preciselynpos, but comparing the result with-1works only because of that conversion and produces a compiler warning.It confuses “not found” with position 0, which is that of an occurrence at the beginning of the string.It is an honest answer: lesson 7.1 presentsfindandnpos.Question 8What does
std::println("{}", std::string{"año"}.size());write in a program saved in UTF-8, like those of this course?It counts letters;sizecounts the bytes in which they are written.It believes that every letter takes two bytes; the letters of ASCII take one.It is an honest answer: lesson 7.2 explains it byte by byte.sizecounts bytes, not letters: in the Spanish word “año” (year), the “a” and the “o” take one each, and the “ñ”, which is not in ASCII, takes two in UTF-8. The lesson Text is not ASCII explains why and teaches how to count the letters.Review: Text is not ASCII
Question 9A program executes
int age{}; std::string name{}; std::cin >> age; std::getline(std::cin, name);and the user types36, presses Enter, typesAnaand presses Enter. What is left inname?It assumes thatgetlinestarts on the next line; it starts where>>stopped.It believes thatgetlinereads again what>>already consumed.It is an honest answer: lesson 7.3 begins with this defect.>>reads the 36 and leaves in the stream the line break that followed it;std::getlinereads up to the first line break, which is that one, and returns what came before it: nothing. The lesson Reading well explains this and avoids it withstd::ws.Review: Reading well
Question 10A program executes
std::ifstream in{"data.txt"}; int x{-1}; in >> x;, butdata.txtdoes not exist in the folder from which it is run. What happens?It assumes that opening a nonexistent file is an error that C++ reports; the program continues without warning.It confuses opening for reading with opening for writing, which does create the file.It is an honest answer: lesson 7.4 begins with this check.Opening a file can fail, and the failure can be seen only by querying the stream: that is whyif (!in)is written before reading. The lesson Text files makes that check a rule and explains which folder the search depends on.Review: Text files
Loading the content…
Completed
Sign in to save your progress