Loading...

C++ Writing a File

The files page left one line on the screen in box.cpp: pine, after getline from a hand-made note.txt. This page stays in the Errors and Files module on purpose. The workshop file on disk stays box.cpp so you keep one familiar build path. The new work opens note.txt for output with ofstream and writes the line pine itself. The screen stays empty on purpose.
An output file stream stores letters on disk instead of printing them. On this page you write std::ofstream out("note.txt"); and then out << "pine" << std::endl;. Reading ofstream is reading a stream that creates or replaces that file name. Reading out << "pine" is reading the store into the file. There is no std::cout line in the checked body, so the terminal stays blank while note.txt holds pine.
Programs use file output when you want letters to remain after the program ends. You do not keep the ifstream and getline path as the lesson body on this page. Extra filesystem path tools wait for the next page on purpose. Here the point is only writing pine into note.txt and noticing the empty screen. The build command stays familiar: one source file box.cpp, one output name box, one run that prints nothing. Keep #include <fstream> at the top. You can keep #include <iostream> unused or drop it; the checked file keeps only <fstream>. You compile only box.cpp again for this workshop file on disk.
Think of writing as filling the sticky note from the program. The previous page asked you to create the note by hand. This page lets box create or replace that note. Open note.txt in an editor after the run to see pine. Do not expect pine on the terminal. The empty screen is part of the lesson.

Inside main, write std::ofstream out("note.txt");, then out << "pine" << std::endl;, then return 0;. Keep #include <fstream> at the top of the file. Do not call std::cout in the checked body. Do not read the file back on this page.
Walk the run carefully from the first line of main. The program starts at main. ofstream opens note.txt for writing. The << store puts pine into the file. Then main returns and the program ends. There is no printed line on the screen. The letters live in note.txt instead. The checked file always writes and never prints.
Say the steps out loud once while you read the file. Open note.txt for output. Write pine. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No getline. No cout. No filesystem path helpers yet. When the screen stays empty and the text file holds pine, the story matched the source.

If you add std::cout << "pine" by mistake, the screen shows pine and the lesson about an empty screen is lost. That experiment can stay in the prose. The checked file never prints.
If you forget #include <fstream>, the build fails about std::ofstream. The checked file keeps that include.
A common mistake is to expect the written text on the terminal. Another common mistake is to keep the old ifstream body and never write. A third mistake is to invent a full path API on this page. A fourth mistake is to wait for cin. A fifth mistake is to open the file only for reading and never use ofstream.
This page stops before the next tool, where filesystem helpers work with paths. The next page names that tool once at the end. The write you have just written only shows storing pine in note.txt. Keep the screen empty on purpose so the order stays clear for a first careful reading of the file.
Remember that writing does not print by itself. You still open the text file in an editor to confirm pine. After a successful run the screen is empty and the disk file holds pine. After you undo a cout experiment the checked file returns to a quiet terminal. Stability of the checked file matters as much as learning ofstream itself.

Open box.cpp. Remove the ifstream and getline lines from the read demo. Keep or add #include <fstream>. In main, create ofstream out("note.txt"), write pine, and return. The program file name on disk stays box.cpp. The checked file is below, and the new lines are the ofstream open and the write.

box.cpp writes pine into note.txt

cpp
#include <fstream>

int main()
{
    std::ofstream out("note.txt");
    out << "pine" << std::endl;
    return 0;
}

Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box from the folder where you want note.txt to appear. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen stays empty.
Check the empty screen first. Then open note.txt in an editor and confirm the first line is pine. The table gives each row its piece, so compare your run with it line by line before you move on to experiments. If the screen shows pine, a cout leftover is still there. If the build fails about ofstream, check #include <fstream>. If note.txt is missing after the run, check that you ran box in the folder you are inspecting.

📊 file write and what the screen shows

Piece What the screen shows
First empty, because writing goes to note.txt not to cout
Last empty, because the program returns with no print
Rule ofstream opens a disk name for output; << stores the line pine
Screen empty

Try one change at a time. Temporarily write oak instead of pine, rebuild, and open note.txt to see oak. Restore pine. Next, temporarily add std::cout << "pine" << std::endl; after the write so the screen shows pine, then remove that print so the checked screen is empty again. Each experiment changes one thing on purpose, so you see either a different file line or a print leftover. Undo each change before the next one starts, so the file always returns to the checked workshop version with ofstream writing pine and no cout once again after every single careful try back on disk again.
This page ends the writing-files tool and stops before the next one. The next page is filesystem, which works with paths and filesystem helpers. The write you have just written only shows storing pine in note.txt from box.cpp. You leave with an empty screen and a file that holds pine. Count the printed lines before you leave. There are exactly zero printed lines on that final screen, and the disk file still holds just pine alone right there now after the final successful build run on disk today now after the ofstream write.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top