[go: up one dir, main page]

0% found this document useful (0 votes)
15 views13 pages

Files - CPP

The document contains two C++ programs that prompt the user to input a specified number of integers and store them in a file named 'numbers.txt'. Both programs check if the file can be created or opened successfully before writing the numbers. After storing the numbers, they confirm the action to the user.

Uploaded by

nani.647000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

Files - CPP

The document contains two C++ programs that prompt the user to input a specified number of integers and store them in a file named 'numbers.txt'. Both programs check if the file can be created or opened successfully before writing the numbers. After storing the numbers, they confirm the action to the user.

Uploaded by

nani.647000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

#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 :

You might also like