File output using ofstream
π Scenario: You are creating a simple program to save a list of favorite fruits to a text file. This is useful when you want to keep a record that can be opened later or shared.
π― Goal: Build a C++ program that writes a list of fruits to a file called fruits.txt using ofstream.
π What You'll Learn
Create a vector of strings named
fruits with the exact values: "Apple", "Banana", "Cherry"Create an
ofstream object named file to open fruits.txtUse a
for loop with variable fruit to write each fruit followed by a newline to the fileClose the file after writing
Print
"Fruits saved to fruits.txt" to the consoleπ‘ Why This Matters
π Real World
Saving data to files is common in many programs, like saving user preferences, logs, or reports.
πΌ Career
Understanding file output is essential for software developers working with data storage, logging, and configuration files.
Progress0 / 4 steps