Save and Load a List of Numbers from a File in C++

In this exercise, you will create a C++ program that allows the user to input a list of numbers, save them to a text file, and then read and display those numbers from the file. This task will help you understand basic file operations such as writing to and reading from a file using C++. File I/O is an essential skill for storing data persistently in applications. You will use `ofstream` to write the numbers and `ifstream` to read them back. This kind of operation is often used in applications that require logging data or saving user input for later use.

Group

File Management in C++

Objective

1. Prompt the user to enter how many numbers they want to save.
2. Use a loop to collect the numbers from the user and store them in a file using `ofstream`.
3. After saving the numbers, reopen the file using `ifstream`.
4. Read the numbers from the file and display them on the console.
5. Make sure to close the file after each operation.

Implement a program that saves a list of numbers to a file and then reads them.

Example C++ Exercise

 Copy C++ Code
#include <iostream>     // Include for standard I/O
#include <fstream>      // Include for file stream operations
using namespace std;

int main() {
    string fileName = "numbers.txt"; // File to store the numbers
    int count;                       // Number of values to enter

    // Ask user how many numbers they want to enter
    cout << "How many numbers do you want to save? ";
    cin >> count;

    ofstream outFile(fileName); // Open file for writing

    // Check if the file was opened successfully
    if (!outFile) {
        cout << "Error opening file for writing." << endl;
        return 1; // Exit with error
    }

    // Loop to write numbers to the file
    cout << "Enter " << count << " numbers:" << endl;
    for (int i = 0; i < count; ++i) {
        int num;
        cin >> num;
        outFile << num << " "; // Write each number followed by a space
    }

    outFile.close(); // Close the output file

    ifstream inFile(fileName); // Open file for reading

    // Check if the file was opened successfully
    if (!inFile) {
        cout << "Error opening file for reading." << endl;
        return 1; // Exit with error
    }

    // Read and display the numbers from the file
    cout << "Numbers read from file:" << endl;
    int num;
    while (inFile >> num) {
        cout << num << " "; // Output each number
    }

    inFile.close(); // Close the input file
    cout << endl;

    return 0; // End of program
}

 Output

How many numbers do you want to save? 4
Enter 4 numbers:
12
34
56
78
Numbers read from file:
12 34 56 78 

Share this C++ Exercise


More C++ Programming Exercises of File Management in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Count the Number of Lines in a Text File Using C++

    This exercise guides you through the process of writing a C++ program that reads a text file and counts the number of lines it contains. This is a fundamental operation when analyz...

  • Sort the Lines of a Text File Using C++

    This exercise involves creating a C++ program that reads all lines from a text file, sorts them in alphabetical order, and then displays the sorted lines on the screen. Sorting fil...

  • Delete a Specific File Using C++

    In this exercise, you will write a C++ program that deletes a specific file from the system using file handling operations. Managing files programmatically is an essential skill in...

  • ATM Transaction Logger in C++

    In this exercise, you will develop a simple ATM simulator in C++ that logs every transaction to a file. Logging is a vital component in real-world applications, especially in banki...

  • Read and Display a Text File Using C++

    In this C++ exercise, you will create a program that reads the contents of a text file and prints them to the console. This task demonstrates how to work with file input streams in...

  • Register User Information and Save It to a File in C++

    In this C++ exercise, you will create a program that collects basic user information—specifically, their name and age—and writes it into a text file. This task introduces you to fi...