Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+;中向.txt文件添加无限数量的记录+;_C++ - Fatal编程技术网

C++ 如何在C+;中向.txt文件添加无限数量的记录+;

C++ 如何在C+;中向.txt文件添加无限数量的记录+;,c++,C++,我是新的编码,我正在创建“电话簿”程序。我想将记录添加到.txt文件,现在一切正常。当我想向文件中添加另一条记录时,它会删除上一条记录并只保存最后一条输入的记录。我想储存所有的记录,但不知道怎么做 这是我的代码,这是头文件: #pragma once #include <string> #include <iostream> #include <fstream> #include <ostream> class Person { public:

我是新的编码,我正在创建“电话簿”程序。我想将记录添加到.txt文件,现在一切正常。当我想向文件中添加另一条记录时,它会删除上一条记录并只保存最后一条输入的记录。我想储存所有的记录,但不知道怎么做

这是我的代码,这是头文件:

#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;
    }
};
#pragma一次
#包括
#包括
#包括
#包括
班主任
{
公众:
std::stringfirstname;
std::字符串lastName;
std::字符串编号;
std::字符串EGN;
人()
{
init();
}
void init()
{
firstName=“”;
lastName=“”;
数字=”;
EGN=“”;
}
std::string getFirstName()常量
{
返回名字;
}
std::string getLastName()常量
{
返回姓氏;
}
std::string getNumber()常量
{
返回号码;
}
std::string getEGN()常量
{
返回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(常数std::string和pin)
{
EGN=引脚;
}
静态void addRecord();
静态无效搜索记录();
静态void deleteRecord();
静态void ShowRecord();
friend std::ostream&operator>EGN1;
对象setFirstName(firstName1);
对象setLastName(lastName1);
对象设置编号(编号1);
obj.setEGN(EGN1);
返回;
}
};
这就是功能:

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括“pch.h”
#包括“Person.h”
bool isDigit(常量std::string和str)
{
返回str.find_first_not_of(“0123456789”)==std::string::npos;
}
void Person::addRecord()
{
std::stringfirstname;
std::字符串lastName;
std::字符串编号;
std::字符串EGN;
个人新纪录;
const std::string filename=“Input.txt”;
std::cout setLastName(lastName);
std::cout newRecord.number;
//新建记录->设置编号(编号);
std::cout newRecord.EGN;
if(isDigit(EGN))
{
//newRecord->setEGN(EGN);
打破
}
其他的
{

std::cout每次调用
addRecord
时,您都在重写输出文件。相反,您应该附加到文件中,如下所示:

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

每次调用
addRecord
时,您都在重写输出文件。相反,您应该附加到文件中,如下所示:

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

使用文件打开模式:追加

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

使用文件打开模式:追加

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

打字比我快:)打字比我快:)