#include <iostream>
#include <fstream>
using namespace std;
int main()
ofstream outFile("numbers.txt"); // Create or open the file
if (!outFile)
cout << "File could not be created!" << endl;
return 1;
int number, count;
cout << "How many numbers do you want to store? ";
cin >> count;
for (int i = 0; i < count; ++i)
cout << "Enter number " << i + 1 << ": ";
cin >> number;
outFile << number << endl; // Write the number to file
outFile.close(); // Close the file
cout << "Numbers stored in 'numbers.txt'." << endl;
return 0;
Output:
How many numbers do you want to store? 3
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Numbers stored in 'numbers.txt'.
#include <iostream>
#include <fstream>
using namespace std;
int main()
ofstream outFile; // Declare output file stream object
// Open the file using open()
outFile.open("numbers.txt");
// Check if file is successfully opened
if (!outFile)
cout << "File could not be opened!" << endl;
else
int number, count;
cout << "How many numbers do you want to store? ";
cin >> count;
for (int i = 0; i < count; ++i)
cout << "Enter number " << i + 1 << ": ";
cin >> number;
outFile << number << endl; // Write number to file
outFile.close(); // Close the file
cout << "Numbers have been stored in 'numbers.txt'." << endl;
return 0;
}
Output :