1/51
Looks like no tags are added yet.
Three main steps of file processing
Open the file\n2. Read/write data\n3. Close the file
Header file required for file processing in C++
#include <fstream>
Comparison of ifstream, ofstream, and fstream in C++
ifstream: Input from file\n- ofstream: Output to file\n- fstream: Input and output
Syntax for declaring input and output file stream objects in C++
ifstream infile;\nofstream outfile;
Function of the open() stream method in C++
Establishes a connection between the program and a file on disk.
Syntax to open a file named inventory.dat for reading in C++
infile.open("inventory.dat");
Syntax to open a file named report.txt for writing in C++
outfile.open("report.txt");
Result of opening a non-existent file for reading using ifstream
The file stream fails to open and enters an error state.
Effect of opening an existing file with default ofstream
The existing file contents are erased (truncated) before writing new data.
Stream operator used to write data to a file in C++
<< (insertion operator)
Resulting file content after executing: ofstream outputFile; outputFile.open("demo.txt"); outputFile << "I love C++" << endl;
A file named demo.txt containing the text: I love C++
Requirements for the Random Number File Assignment
Generate 100 random numbers ranging from 30 to 80, writing 10 numbers per line to a file.
Requirements for the Count-The-Numbers Assignment
Count total integers in a file, print numbers from 1 to count−10, and display 20 numbers per line.
Requirements for the Read/Write Assignment
Read 10 integers, calculate their average, minimum, and maximum, and write the output to a file.
Requirements for the Paragraph Analysis Assignment
Read a paragraph from a file and count the total characters, words, and sentences.
Definition of an array
A data structure that stores multiple values of the same data type in contiguous memory.
Fundamental rule that every array must follow
All elements stored in the array must be of the exact same data type.
Physical arrangement of array elements in memory
Elements are stored in adjacent (contiguous) memory locations.
C++ syntax to declare an integer array named tests with 5 elements
int tests[5];
Maximum capacity of elements in int tests[5];
5 integer elements
Difference between declaring int tests[5]; vs int x;
tests stores 5 integers in contiguous memory, whereas x stores a single integer value.
Memory allocation produced by int tests[5];
Allocates memory for 5 consecutive integer locations.
Total element count in int numbers[12];
12 elements
Total element count in double values[25];
25 elements
Error present in: int data[3]; data[0] = 10; data[1] = "Hello";
Type mismatch error: string "Hello" cannot be stored in an array declared for integers.
Primary practical benefit of using arrays
Enables storing and managing many related values under a single variable name.
Can a standard C++ array store a mix of different data types?
No, every element in an array must share the exact same data type.
Data structure that stores elements of a single type in sequential memory addresses
An array
Two defining properties of an array
Definition of parallel arrays
Two or more arrays of equal size that store related properties of items using matching index values.
Data fields stored in the parallel array example
Student IDs, Names, Courses, and Grades
Method used to connect related records across parallel arrays
Elements sharing the same index across different arrays refer to the same entity.
Array location of John's grade if his name is stored in name[2]
grade[2]
Array location of a student's course if their student ID is in id[4]
course[4]
Main reason to use parallel arrays
To link different pieces of information about an entity without defining custom structures or classes.
Entity represented by index 0 in a set of parallel arrays
The first item or record across all parallel arrays.
Most critical rule for maintaining parallel arrays
Matching information for an entity must always remain at the same index across all arrays.
Definition of a two-dimensional array
An array structure organized into a grid of rows and columns.
Real-world structure analogous to a 2D array
A table or spreadsheet.
C++ syntax to declare a 2D integer array with 4 rows and 3 columns
const int ROWS = 4; const int COLS = 3; int exams[ROWS][COLS];
Dimension represented by the first size parameter in a 2D array declaration
The number of rows.
Dimension represented by the second size parameter in a 2D array declaration
The number of columns.
Total element count in int exams[4][3];
4×3=12 elements
General syntax for indexing an element in a 2D array
arrayName[rowIndex][columnIndex]
Action performed by the statement exams[2][2] = 86;
Assigns the value 86 to the cell at row index 2 and column index 2.
Location accessed by scores[1][3]
Row index 1, Column index 3
Location accessed by scores[2][0]
Row index 2, Column index 0
Action performed by the statement exams[1][0] = 50;
Updates the element at row index 1, column index 0 to 50.
Can a 2D array combine string and integer elements?
No, all elements in a 2D array must share the exact same data type.
Why player names and numeric stats cannot coexist in one numeric 2D array
Names are strings and stats are numeric; arrays mandate uniform data types.
Can player statistics alone be stored in a 2D array?
Yes, because all numeric statistics share the same data type.
Condition for choosing a 2D array over parallel arrays
When data naturally forms a uniform grid where every row and column holds the exact same data type.