2014-01-19 2 views
0

저는 임금 신청서를 작성 중입니다. 응용 프로그램에서는 사용자가 계정 (금액이 1000 인 텍스트 파일 "shop"인 계정)에서 금액을 이체 할 수 있어야합니다.파일에 타임 스탬프 쓰기

사용자는 계정을 과도하게 사용하지 않고 원하는만큼 많은 이전을 할 수 있어야합니다. 각 트랜잭션은 별도의 파일에 타임 스탬프로 기록되어야하며 이것은 내가 고심하는 비트입니다.

현재 코드를 사용하고 있는데 타임 스탬프는 1040ED48을 제외하고 "time"파일에서 잘 생성됩니다. 아무도 이것이 왜 있는지 압니까? 또한 트랜잭션을 수행 할 때마다 "시간"파일이 새 타임 스탬프로 덮어 쓰여집니다. 파일의 다른 줄에 타임 스탬프를 두어 파일을 덮어 쓰지 못하도록 막을 수 있습니까? 이것이 명확하지 않으면 죄송합니다.

#include <limits> 
#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <ctime> 
#include <string> 


int read_balance(void); 
void write_balance(int balance); 

using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 


    cout << "How much do you wish to transfer?" << endl; 


    int amount = 0; 

if (std::cin >> amount) 
{ 
    std::cout << "Transferred Amount:" << amount << "\n"; 
    int balance = read_balance(); 

    if (amount <= 0) 
    { 
     std::cout << "Amount must be positive\n"; 
    } 
    else if (balance < amount) 
    { 
     std::cout << "Insufficient funds\n"; 
    } 
    else 
    { 
     int new_balance = balance - amount; 

     write_balance(new_balance); 
     std::cout << "New account balance: " << new_balance << std::endl; 

    fstream infile; 
    infile.open("time.txt"); 


    std::time_t result = std::time(nullptr); 
    std::string timeresult = std::ctime(&result); 

    infile << std::cout << timeresult << std::endl; 
    } 

} 


system("pause"); 
return 0; 
} 

int read_balance(void) 
{ 
std::ifstream f; 
f.exceptions(std::ios::failbit | std::ios::badbit); 
f.open("shop.txt"); 
int balance; 
f >> balance; 
f.close(); 
return balance; 
} 

void write_balance(int balance) 
{ 
std::ofstream f; 
f.exceptions(std::ios::failbit | std::ios::badbit); 
f.open("shop.txt"); 
f << balance; 
f.close(); 
} 
+0

'std :: cout <<'을'infile << std :: cout << timeresult << std :: endl;에서 제거하십시오 – tumdum

+0

1040ED48을 제거해 주셔서 감사합니다. 이제는 타임 스탬프 seperatley를 기록하는 방법을 알아야합니다. – user3057816

답변

1

파일을 쓰기 위해 열면 해당 파일을 삭제합니다. 이 파일을 삭제하기 위해을 원하지 않으면 추가 할 파일을 열어야합니다 (app 모드 사용).

+0

감사합니다! – user3057816

1

한 가지 더. 오류 상태를 확인한 후 다음을 인쇄해야합니다.

std::cout << "Transferred Amount:" << amount << "\n"; 
int balance = read_balance(); 

ATM에 있다고 가정 해보십시오. 이제 귀하는 귀하의 계좌에 남아있는 금액 이상을 인출하려고 시도하고 ATM에 금액이 송금되어 잔액이 충분하지 않다는 것을 보여줍니다.

관련 문제