User Tools

Site Tools


lecture_materials:week_00

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
lecture_materials:week_00 [2025-08-18 21:59] Jeffrey Bergaminilecture_materials:week_00 [2025-08-19 14:31] (current) Jeffrey Bergamini
Line 14: Line 14:
  
 ==== Scope and Lifetime ==== ==== Scope and Lifetime ====
 +
 +{{ :lecture_materials:blocks-scope-lifetime.pdf |Jeffrey Bergamini: Blocks, Scope and Lifetime}}
  
 === scopes.cpp (illustrating locally and globally scoped variables) === === scopes.cpp (illustrating locally and globally scoped variables) ===
Line 61: Line 63:
 == Notes == == Notes ==
  
-Your job: Be the compiler and the OS and determine the output without using any tools but your mind!+Your job: Be the compiler and the shell and determine the output of this program without using any tools other than your mind!
  
 Then compile and run this code to see if you were right. Then compile and run this code to see if you were right.
Line 117: Line 119:
 === raii.cpp (illustrating the concept of RAII) === === raii.cpp (illustrating the concept of RAII) ===
  
-<file cpp scopes_annotated.cpp [enable_line_numbers=true]>+<file cpp raii.cpp [enable_line_numbers=true]>
 /** /**
  * @file raii.cpp  * @file raii.cpp
Line 161: Line 163:
   std::ifstream othello{"/srv/datasets/shakespeare-othello.txt"};   std::ifstream othello{"/srv/datasets/shakespeare-othello.txt"};
   std::cout << "Othello: " << count_vowels(othello) << '\n';   std::cout << "Othello: " << count_vowels(othello) << '\n';
-  // std::cout is just a plain input stream:+  // std::cin is just a plain input stream:
   std::cout << "std::cin: " << count_vowels(std::cin) << '\n';   std::cout << "std::cin: " << count_vowels(std::cin) << '\n';
   // Lifetimes of `hamlet` and `othello` end here.   // Lifetimes of `hamlet` and `othello` end here.
Line 184: Line 186:
  */  */
  
-#include <cctype>    // `std::isprint()` +#include <cctype>    // for `std::isprint()` 
-#include <fstream>   // `std::ifstream` +#include <fstream>   // for `std::ifstream` 
-#include <iostream>  // `std::cout`+#include <iostream>  // for `std::cout`
  
 +/**
 + * `main()` can have parameters to access *command-line arguments* and *environment variables*.
 + *
 + * @param argc argument count: the number of command-line arguments (including the executable name)
 + * @param argv argument values: an array of C strings containing the command-line arguments
 + */
 int main(int argc, char **argv) { int main(int argc, char **argv) {
 +  // Exit with an error message if command-line arguments weren't provided:
 +  if (argc < 3) {
 +    std::cerr << "Usage: " << argv[0] << " FILE1 FILE2\n";
 +    return 1;  // nonzero exit status reports unsuccessful termination to the shell
 +  }
 +  // Attempt to open the two files specified as command-line arguments:
   std::ifstream f1(argv[1]);   std::ifstream f1(argv[1]);
   std::ifstream f2(argv[2]);   std::ifstream f2(argv[2]);
-  // TODO: Find all the printable characters unique present in one file and not the other. +  if (!f1 || !f2) { 
-  // Print each unique character and its ASCII number, one character per line, in ascending order.+    std::cerr << "Cannot read from input file(s)\n"; 
 +    return 2; 
 +  } 
 +  // TODO: Find all the unique printable characters present in one file and not the other. 
 +  // Print each unique character and its ASCII encoding, one character per line, in ascending order.
 } }
 </file> </file>
lecture_materials/week_00.1755579575.txt.gz · Last modified: by Jeffrey Bergamini