Alexandar Zhelev
Alexandar Zhelev

Reputation: 17

How to add unlimited count of records to .txt file in C++

I'm new to coding and I'm creating "Phone book" program. I want to add records to .txt file and for now everything is fine. When I want to add another record to file, it deletes the previous one and save only the last one entered. I want to store all records but have no idea how to do that.

Here is the peace of my code, this is header file:

#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include <ostream>

class Person
{
public:
    std::string firstName;
    std::string lastName;
    std::string number; 
    std::string EGN;    

    Person() 
    {
        init();
    }

    void init()
    {
        firstName = "";
        lastName = "";
        number = "";
        EGN = "";
    }

    std::string getFirstName() const
    {
        return firstName;
    }

    std::string getLastName() const
    {
        return lastName;
    }

    std::string getNumber() const
    {
        return number;
    }

    std::string getEGN() const
    {
        return EGN;
    }

    void setFirstName(const std::string& fn)
    {
        firstName = fn;
    }

    void setLastName(const std::string& ln)
    {
        lastName = ln;
    }

    void setNumber(const std::string& num)
    {
        number = num;
    }

    void setEGN(const std::string& pin)
    {
        EGN = pin;
    }

    static void addRecord();
    static void SearchRecord();
    static void deleteRecord();
    static void ShowRecord();
    
    friend std::ostream & operator << (std::ostream &out, const Person & obj)
    {
        out << obj.getFirstName() << "\n" << obj.getLastName() << "\n" << obj.getNumber() << "\n" << obj.getEGN() << std::endl;

        return out;
    }

    friend std::istream & operator >> (std::istream &in, Person &obj) 
    {
        std::string firstName1;                     
        std::string lastName1;                      
        std::string number1;
        std::string EGN1;

        in >> firstName1;
        in >> lastName1;
        in >> number1;
        in >> EGN1;

        obj.setFirstName(firstName1);
        obj.setLastName(lastName1);
        obj.setNumber(number1);
        obj.setEGN(EGN1);

        return in;
    }
};

and this is the function:

#include <fstream>
#include <iostream>
#include <string>
#include <ostream>
#include <iterator>
#include <sstream>

#include "pch.h"
#include "Person.h"

bool isDigit(const std::string &str)
{
    return str.find_first_not_of("0123456789") == std::string::npos;
}

void Person::addRecord()
{
    std::string firstName;
    std::string lastName;
    std::string number;
    std::string EGN;

    Person newRecord;
    const std::string filename = "Input.txt";

    std::cout << "Enter information" << "\n";

    std::cout << "Enter first name:" << "\n";
    std::cin >> newRecord.firstName;
    //newRecord->setFirstName(firstName);

    std::cout << "Enter last name:" << "\n";
    std::cin >> newRecord.lastName;
    //newRecord->setLastName(lastName);

    std::cout << "Enter phone number:" << "\n";
    std::cin >> newRecord.number;
    //newRecord->setNumber(number);

    std::cout << "Enter EGN:" << "\n";
    while (true)    //check PIN validation
    {
        std::cin >> newRecord.EGN;

        if (isDigit(EGN))
        {
            //newRecord->setEGN(EGN);
            break;
        }

        else
        {
            std::cout << "Enter valid EGN and try again!" << "\n";
            std::cin.clear();
        }
    }

    // Open file to write
    std::ofstream out_file(filename);

    // Write data to file
        out_file << newRecord;
    
    // Close file
    out_file.close();

    // Clear the object to be reused
    //newRecord.init();

    // Open file to read
    std::ifstream in_file(filename);

    // Read file content
    in_file >> newRecord;
}

Upvotes: 1

Views: 100

Answers (2)

cigien
cigien

Reputation: 60412

You are rewriting the output file every time you call addRecord. Instead, you should append to the file, like this:

// Open file to write
std::ofstream out_file(filename, std::ofstream::app | std::ofstream::out);

Upvotes: 3

Amir Kadyrov
Amir Kadyrov

Reputation: 1288

Use file open mode: append

std::ofstream(filename, std::ofstream::app);

Upvotes: 1

Related Questions