簡體   English   中英

C++ 幫助 - 在函數中使用 Ctime 為變量添加時間戳

[英]C++ Help - Using Ctime in a function to timestamp variables

全部! 我正在嘗試找到接受用戶輸入的最佳方式,為其添加時間戳,然后將其放入具有正確格式的文件中(每行一個時間戳/輸入。時間戳的最佳方法是什么,然后將其全部放入文件?謝謝!

以我的准系統代碼為例:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;

//void passwordCheck()
//{
//
//}

string timestamp(string passwordInput1, string passwordInput2, string passwordInput3, string passwordInput4, string passwordInput5, string passwordInput6, string passwordInput7, string passwordInput8, string passwordInput9, string passwordInput10)
{
time_t now = time(0);

char* dt = ctime(&now);

cout << "The local date and time is: " << dt << endl;

tm *gmtm = gmtime_s(&now);
dt = asctime(gmtm);

}

//void saveToFile()
//{
//
    //cout << "I've saved 10 passwords for you." << endl;
//}

int main()
{
    ofstream outputToFile;
    outputToFile.open("manageMyPasswords.txt");

    // Look for easier/cleaner way to do this.
    string passwordInput1, passwordInput2, passwordInput3, passwordInput4, passwordInput5, passwordInput6, passwordInput7, passwordInput8, passwordInput9, passwordInput10;

    // Put into a function then call back here.
    cout << "Welcome to Password Manager!" << endl;
    cout << "     Enter your first password" << endl;
    getline (cin, passwordInput1);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput2);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput3);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput4);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput5);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput7);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput8);
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput9); 
    cout << "Enter the next password." << endl;
    getline (cin, passwordInput10);


    //void saveToFile();







    system("pause");
    return 0;
}

據我了解你的問題,你需要這樣的東西:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <map>

int main(int argc, char** argv)
{
    typedef std::multimap<time_t, std::string> Passwords;
    Passwords passwords;

    short counter = 1;

    std::cout << "Enter passwords or \"quit\" to stop" << std::endl;
    for (std::string line;;)
    {
        std::cout << "Enter password " << counter << " -> ";
        getline(std::cin, line);
    
        if (line.empty())
        {
            continue;
        }
        else if (line.compare("quit") == 0)
        {
            break;
        }

        passwords.insert(std::pair<time_t, std::string>(time(0), line));

        counter++;
    }

    std::ofstream outputFile;
    outputFile.open("manageMyPasswords.txt", std::ios::trunc);

    for (Passwords::const_iterator it = passwords.begin(); it != passwords.end(); it++)
    {
        outputFile << it->first << " " << it->second << "\n";
    }

    outputFile.close();

    return 0;

}

它將無限等待用戶輸入,直到沒有輸入“退出”。 之后它將逐行轉儲帶有密碼的時間戳。

PS 我使用了multimap ,因為你可以有重復的時間戳。

更新

從您的代碼看來,您需要人類可讀的時間。 使用此函數檢索當前日期和時間:

// Returns current date-time formatted like [YYYY-MM-DD][HH:mm:ss]
const std::string GetCurrentDateTime()
{
    time_t currentTime = time(0);
    struct tm localDateTime;
    char buf[80];

    localtime_s(&localDateTime, &currentTime);
    strftime(buf, sizeof(buf), "[%Y-%m-%d][%X]", &localDateTime);

    return buf;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM