简体   繁体   English

C++ 帮助 - 在函数中使用 Ctime 为变量添加时间戳

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

all!全部! I'm trying to find the best way to accept user input, timestamp it, then place it into a file with correct formatting (One timestamp/input per line. What is the best way to timestamp then give over to put it all in a file? Thanks!我正在尝试找到接受用户输入的最佳方式,为其添加时间戳,然后将其放入具有正确格式的文件中(每行一个时间戳/输入。时间戳的最佳方法是什么,然后将其全部放入文件?谢谢!

Heres my barebones code as an example:以我的准系统代码为例:

#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;
}

As I understood your question you need sth like this:据我了解你的问题,你需要这样的东西:

#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;

}

It will wait for users input infinitely, until "quit" is not entered.它将无限等待用户输入,直到没有输入“退出”。 After that it will dump timestamps with password line by line.之后它将逐行转储带有密码的时间戳。

PS I've used multimap , because you can have duplicate timestamps. PS 我使用了multimap ,因为你可以有重复的时间戳。

Update更新

It seems from your code that you need human-readable time.从您的代码看来,您需要人类可读的时间。 Use this function to retrieve current date and time:使用此函数检索当前日期和时间:

// 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