Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.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++;帮助-在函数中使用Ctime为变量添加时间戳_C++_Visual Studio_Intermediate Code - Fatal编程技术网

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

C++ C++;帮助-在函数中使用Ctime为变量添加时间戳,c++,visual-studio,intermediate-code,C++,Visual Studio,Intermediate Code,全部! 我正试图找到最好的方法来接受用户输入,给它加上时间戳,然后将它放入一个格式正确的文件中(每行一个时间戳/输入)。什么是给它加上时间戳,然后把它全部放在一个文件中的最好方法?谢谢 以下是我的裸体代码示例: #include <iostream> #include <fstream> #include <string> #include <ctime> using namespace std; //void passwordCheck()

全部! 我正试图找到最好的方法来接受用户输入,给它加上时间戳,然后将它放入一个格式正确的文件中(每行一个时间戳/输入)。什么是给它加上时间戳,然后把它全部放在一个文件中的最好方法?谢谢

以下是我的裸体代码示例:

#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//无效密码检查()
//{
//
//}
字符串时间戳(字符串密码输入1、字符串密码输入2、字符串密码输入3、字符串密码输入4、字符串密码输入5、字符串密码输入6、字符串密码输入7、字符串密码输入8、字符串密码输入9、字符串密码输入10)
{
现在时间=时间(0);
char*dt=ctime(&now);

我理解你的问题,你需要这样的东西:

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

}

您面临的问题是什么?“最好的方法是什么”无法客观地回答。我很难在从用户收到的输入上加盖时间戳,然后将带有输入的时间戳发送到文件。“我有问题”不是一个可接受的问题描述。拜托!哥们,我不知道如何给我收到的输入加上时间戳,这样我就可以把它放进一个文件中。我不是你的“哥们”。现在放弃。祝你好运。
typedef
?如果你不使用C++11,多重映射不能保证重复的顺序,因此如果发生重复,你的多重映射方法实际上可能会适得其反。似乎
std::vector
就足够了。这完全符合我的需要,我没想到会这样做,非常感谢!!
// 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;
}