9.03 Input-to-Output Stream Transformations & Circular Ring Logging in EEPROM
Executive Summary: Building read-transform-write file pipelines. We analyze streaming mathematical transformation of files and compare file-based streams with circular logging queues stored in non-volatile I2C/SPI EEPROM memory.
💻 1. Annotated Source Code
#include <iostream> #include <fstream> using namespace std; int main() { ifstream infile("input.txt"); ofstream outfile("output.txt"); if (!infile) { cerr << "Error opening input file" << endl; return 1; } int tempNum; while (infile >> tempNum) { outfile << (tempNum * 2) << endl; } infile.close(); outfile.close(); cout << "Doubled values written to output.txt!" << endl; return 0; }
10 12 14 22 51 35 555 32 15 182 22
20 24 28 44 102 70 1110 64 30 364 44
📐 2. Architecture & UML Class Model
<<compilation-unit>>
TwiceFileProcessor
Stream Pipeline
Attributes / Data Members
-inFilePath : const char*
-outFilePath : const char*
Operations / Methods
+processDoubling(inFile: const char*, outFile: const char*) : bool
📚 3. Core C++ Concepts Deep-Dive
1. Streaming Data Pipelines
Reading elements sequentially from an input stream, applying a transformation function, and writing directly to an output stream in $O(1)$ memory space.
⚡ 4. Embedded Systems & Hardware Reality
1. Non-Volatile EEPROM Circular Buffers
In industrial sensors, small I2C EEPROMs (e.g. 24LC256 - 32KB) store error event logs. Because EEPROM allows byte-level writes with 1,000,000+ erase endurance, circular ring structures log telemetry indefinitely.
💡 5. Production-Ready Embedded Refactoring
Streaming data transformer:
💡 Production-Ready Refactor
#include <cstdint> #include <cstddef> // In-place streaming sensor sample scaler void scaleSensorStream(const int16_t* in_samples, int16_t* out_samples, size_t count, int16_t multiplier) noexcept { for (size_t i = 0; i < count; ++i) { out_samples[i] = in_samples[i] * multiplier; } }
📝 Knowledge Verification Quiz
Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.
Q1. How does I2C/SPI EEPROM differ from NOR Flash memory in embedded hardware?
Detailed Explanation:
EEPROM allows erasing and writing individual bytes independently, making it ideal for parameter storage and high-frequency event logging.
Q2. What is the memory complexity of a streaming data transformation pipeline that processes items one at a time?
Detailed Explanation:
Processing items one by one in a stream pipeline requires only a single element buffer, operating in $O(1)$ RAM.
Q3. What happens if a stream extraction 'in_file >> val' reaches End-Of-File (EOF)?
Detailed Explanation:
Upon reaching EOF,
eofbit is set, causing while (in_file >> val) loops to terminate cleanly.
Q4. Why is closing file streams explicitly (or via RAII scope exit) critical before reading the destination file?
Detailed Explanation:
Closing a file flushes all remaining cached buffer data to physical media, ensuring readers see complete files.