How to use function params?

Yumiyashi Jan 15, 2015

  1. Yumiyashi

    Yumiyashi © Grey Horse Pr0n Lifetime Gold XPG Retired Staff
    205/282

    Joined:
    May 26, 2013
    Messages:
    321
    Likes Received:
    55
    Trophy Points:
    55
    Console:
    Xbox
    (See Bullet? I uses the 'How to' thingy. :3 )

    New to C++ so my code my be a bit challenged. BTW, I borrowed the timer code from somewhere... not that skilled.

    Anyway I am trying to send the gettime() through the log() as a param so log( gettime() ) so it will write the date/time to a file but I have no idea how to and the msdn info sucks a**. :wallbash:

    Main.cpp

    #include "stdafx.h"
    #include <iostream>

    #include "time.h"
    #include "logs.h"

    using namespace std;

    int main()
    {
    gettime();
    log("PENISFHE");
    getchar();
    }

    Time.h

    #include <time.h>

    int gettime()
    {
    struct tm newtime;
    char am_pm[] = "AM";
    __time64_t long_time;
    char timebuf[26];
    errno_t err;

    // Get time as 64-bit integer.
    _time64(&long_time);
    // Convert to local time.
    err = _localtime64_s(&newtime, &long_time);
    if (err)
    {
    printf("Invalid argument to _localtime64_s.");
    exit(1);
    }
    if (newtime.tm_hour > 12) // Set up extension.
    strcpy_s(am_pm, sizeof(am_pm), "PM");
    if (newtime.tm_hour > 12) // Convert from 24-hour
    newtime.tm_hour -= 12; // to 12-hour clock.
    if (newtime.tm_hour == 0) // Set hour to 12 if midnight.
    newtime.tm_hour = 12;

    // Convert to an ASCII representation.
    err = asctime_s(timebuf, 26, &newtime);
    if (err)
    {
    printf("Invalid argument to asctime_s.");
    exit(1);
    }
    printf("%.19s %s\n", timebuf, am_pm);
    }

    Logs.h

    #include <fstream>
    using namespace std;

    void log(char *c)
    {

    ofstream outfile("test.txt");
    if (outfile.fail()) {
    cout << "Couldn't open the file!" << endl;
    //return 0;
    }
    outfile << c << endl;
    outfile.close();


    }


    I am trying to get log to write the time/date to a file, simple as that. I have almost no idea what I am doing so my code might be a tad challenged. (Still trying to figure out .h/.cpp and what the void does.)
     
  2. Iamcoolz

    Iamcoolz Forum Administrator Staff Member XPG Administrator
    205/282

    Joined:
    Mar 30, 2012
    Messages:
    1,227
    Likes Received:
    507
    Trophy Points:
    205
    Gender:
    Male
    Location:
    XPG
    Console:
    Xbox One
    Trying to do something like this o_O?

    #include <string>
    #include <time.h>

    #define MAX_DATE 14

    std::string get_date(void)
    {
    time_d now;
    char DATE_NOW[MAX_DATE];

    DATE_NOW [0] = '\0';

    now = time(NULL);

    if (now != -1)
    {
    strftime(DATE_NOW, MAX_DATE, "%d_%m_%y", gmtime(&now));
    }

    return std::string(DATE_NOW);
    }

    From my understanding you can change the format's of what you want - http://www.cplusplus.com/reference/ctime/strftime/

    I am not too sure if I understood you completely, this will write the filename with the current date and time.
     

Share This Page

Close