在函数中使用文件后无法写入文件

时间:2019-12-20 02:57:12

标签: c++ file fstream

我对代码感到困惑。首先,我在全局区域中创建一个fstream,并在名为“ Load”的函数中使用它。之后,我使用它向其中写入一些信息,但是它不起作用。我不知道为什么当我不使用第一个功能并且它又能正常工作时。这些代码仅计算要添加的人数并创建相应的文件路径。

// BANK ACCOUNT
#include<iostream>
#include<ios>
#include<limits>
#include<string>
#include<fstream>
using namespace std;
int Number = 0;
fstream FILE_IN;
class BankAccount {
private:
  int accountNumber;
  double accountBalance;
  string password;
  string Filedata_Path;
  string accountname;
  string History;
  fstream FILE_DATA;
public:
  BankAccount(int accountNumber , double accountBalance , string password , string accountname);
  bool EnterAccount(int accountNumber , string password);
  void Transfer(BankAccount account , double amount);
};
BankAccount::BankAccount(int accountNumber , double accountBalance , string password , string accountname)
{
  this->accountNumber = accountNumber;
  this->accountBalance = accountBalance;
  this->password = password;
  this->accountname = accountname;
  this->Filedata_Path = "./BankAccount_Data/Account" + to_string(Number + 1) + ".txt";
  this->FILE_DATA.open("./BankAccount_Data/Account" + to_string(Number + 1) + ".txt" , ios::out);
  this->FILE_DATA << accountname + '\n';
  this->FILE_DATA << accountBalance + '\n';
  this->FILE_DATA << "-H\n0\nH-";
  FILE_IN << this->accountname << '\n' << this->password << endl;
  FILE_DATA.close();
  Number++;
}
bool BankAccount::EnterAccount(int accountNumber , string password)
{

}
void BankAccount::Transfer(BankAccount account1 , double amount)
{

}
void Load(int &Number)
{
  string temp;
  int i = 0;
  while(1)
  {
    FILE_IN >> temp;
    if(FILE_IN.eof())
    break;
    FILE_IN.seekg(1 , ios::cur);
    if(i%2 == 0) Number++;
    i++;
  }
}
int main()
{
    FILE_IN.open("./Bank_Account_File_In.txt" , ios::out | ios::in);
    Load(Number);
    while(1)
    {
      int choose;
      std::cout << "Enter number you want to choose :\n"
      << "1.Create a new bank account\n"
      << "2.Enter a exist account\n"
      << "3.Exit\n";
      std::cin >> choose;
      cin.ignore(numeric_limits<streamsize>::max() , '\n');
      if(choose == 1)
      {
        int account_number;
        double account_balance;
        string password;
        string name;
        std::cout << "Enter the information\n"
        << "Account number:";
        std::cin >> account_number;
        cin.ignore(numeric_limits<streamsize>::max() , '\n');
        std::cout << "\nAccount Name:";
        std::cin >> name;
        cin.ignore(numeric_limits<streamsize>::max() , '\n');
        std::cout << "\nAccount Balance:";
        std::cin >> account_balance;
        cin.ignore(numeric_limits<streamsize>::max() , '\n');
        std::cout << "\nPassword:";
        std::cin >> password;
        cin.ignore(numeric_limits<streamsize>::max() , '\n');
        BankAccount new_account = BankAccount(account_number , account_balance ,password , name);
      }
      if(choose == 3) 
      {
        FILE_IN.close();
        return 0;
      }
    }
}

0 个答案:

没有答案