How do I write a C plus plus program to store my friends name in a file?

1 answer

Answer

1088237

2026-08-02 20:50

+ Follow

To write a C++ program that stores a friend's name in a file, you can use the <fstream> library. First, include the library and create an ofstream object to open a file for writing. Use the << operator to write the friend's name to the file, then close the file. Here's a simple example:

<code class="language-cpp">#include <iOStream>

#include <fstream> using namespace std;

int main() { ofstream outFile("friends.txt"); if (outFile.is_open()) { outFile << "Friend's Name\n"; // Replace with the actual name outFile.close(); } else { cout << "Unable to open file"; } return 0; }

</code>
ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.