0
0
C++programming~10 mins

Reading and writing files in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reading and writing files
Open file for writing
Write data to file
Close file
Open file for reading
Read data from file
Close file
End
The program opens a file to write data, closes it, then opens the same file to read data back, and finally closes it.
Execution Sample
C++
#include <fstream>
#include <iostream>
#include <string>

int main() {
  std::ofstream outFile("data.txt");
  outFile << "Hello, file!";
  outFile.close();

  std::ifstream inFile("data.txt");
  std::string content;
  inFile >> content;
  std::cout << content << std::endl;
  inFile.close();
  return 0;
}
This code writes "Hello, file!" to a file named data.txt, then reads it back and prints it.
Execution Table
StepActionFile StateVariable ValuesOutput
1Open file 'data.txt' for writingdata.txt opened for writing (empty)outFile: open
2Write "Hello, file!" to filedata.txt contains "Hello, file!"outFile: open
3Close output filedata.txt saved with "Hello, file!"outFile: closed
4Open file 'data.txt' for readingdata.txt opened for readinginFile: open
5Read first word from file into contentdata.txt unchangedcontent: "Hello,"
6Print content to consoledata.txt unchangedcontent: "Hello,"Hello,
7Close input filedata.txt closedinFile: closed
8Program endsNo files opencontent: "Hello,"
💡 Program ends after reading and printing the first word from the file.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 6Final
outFilenot openedopenclosedclosedclosed
inFilenot openednot openedopenopenclosed
content"""""Hello,""Hello,""Hello,"
Key Moments - 2 Insights
Why does reading with inFile >> content only read "Hello," and not the whole line?
The extraction operator (>>) reads input until the first whitespace, so it stops after "Hello,". To read the whole line, getline() should be used instead. See execution_table step 5.
Why do we need to close the file after writing before opening it for reading?
Closing the file ensures all data is saved and the file is properly released. Without closing, reading might not see the latest data. See execution_table steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the value of 'content' after reading from the file?
A"Hello,"
B"Hello, file!"
C"file!"
D""
💡 Hint
Check the 'Variable Values' column at step 5 in the execution_table.
At which step is the file closed after writing?
AStep 2
BStep 4
CStep 3
DStep 7
💡 Hint
Look for the action mentioning closing the output file in the execution_table.
If we want to read the entire line "Hello, file!" instead of just "Hello," which change should we make?
AUse inFile >> content as is
BUse getline(inFile, content) instead of inFile >> content
CWrite more data to the file
DClose the file earlier
💡 Hint
Refer to key_moments explaining why only the first word is read.
Concept Snapshot
Reading and writing files in C++:
- Use std::ofstream to open a file for writing.
- Write data using << operator.
- Close the file to save data.
- Use std::ifstream to open a file for reading.
- Read data using >> operator or getline() for full lines.
- Always close files after operations.
Full Transcript
This example shows how a C++ program writes text to a file and then reads it back. First, it opens a file named data.txt for writing using ofstream. It writes the string "Hello, file!" to the file and then closes it to save the data. Next, it opens the same file for reading using ifstream. It reads the first word from the file into a string variable named content using the extraction operator >>. Then it prints this content to the console. Finally, it closes the input file and ends the program. The execution table tracks each step, showing file states and variable values. Key points include that >> reads only up to the first space, so only "Hello," is read, and that files must be closed after writing before reading to ensure data is saved. The visual quiz tests understanding of these steps and common confusions.