0

I want to use the system date I have from the computer and use it as the name of the .txt file. Here's what I have so far:

void idle_time()
{
LASTINPUTINFO last_info;
last_info.cbSize = sizeof(LASTINPUTINFO);
int tickCount = 0;
int minutes = 0;
int count = 0;
SYSTEMTIME a;

while(true)
{
    GetLastInputInfo(&last_info);
    tickCount = GetTickCount();
    int minutes = (tickCount - last_info.dwTime) / 60000;
    count++;

    string date;
    date += a.wMonth;
    date += "/";
    date += a.wDay;
    date += "/";
    date += a.wYear;

    if((minutes >= 1) && (count%3000==0))
    {
        //std::string filename = date;
        //filename += ".txt";
        ifstream in(string(date + ."txt").c_str());
        float sum;
        in >> sum;
        sum++;
        in.close();
        ofstream out(string(date + ".txt").c_str());
        out << sum;
        out.flush();
        out.close();

    }

I'm sorry for the terrible indentation. This editor doesn't do it justice. But anyway, how would I use the date as the filename?

craig65535
  • 3,439
  • 1
  • 23
  • 49
TriX
  • 3
  • 4
  • This is essentially appending an `unsigned short` (as `wMonth` et al are of type `WORD`) to a `std::string`. See this, among many others, for several ways to achieve this http://stackoverflow.com/questions/10516196/append-an-int-to-a-stdstring/10516313#10516313 – hmjd Jul 30 '12 at 20:36

1 Answers1

2

The date string you are using contains / characters which are used to separate directories in path. You simply need to replace them with another (not forbidden) character.

I'd also suggest you not to use m/d/Y date format for filenames. They do not sort well. Y-m-d is usually better because the files will be sorted from oldest to newest.

Edit: ah, and for my last statement to be true, you'd also need to pad month and day with zero to two digits, i.e. have something like 2011-08-05.


Ah, I see that the time appending is also done incorrectly. You can't append integers to strings like that. @hmjd already posted you one method of solving this; but I think it will be better to just use a dedicated time->string conversion method.

I'm not a Windows programmer, so I won't help you much with this SYSTEMTIME thing. You'll probably need to use GetTimeFormat(). But here's a simple example how to solve it with standard C/C++ strftime():

char date_buf[11];
time_t a = time(0);

strftime(date_buf, sizeof(buf), "%Y-%m-%d", gmtime(&t));

date += buf;

If you want the date in local time zone rather than UTC, use localtime() instead of gmtime().

Hope it helps someone. I believe GetTimeFormat() works similarly, so maybe this can guide you a bit.

Michał Górny
  • 18,713
  • 5
  • 53
  • 76
  • I changed "/" to "-". But it's not showing up. Any suggestions still? – TriX Jul 30 '12 at 20:39
  • Hmm, yes, you can't append numbers to strings like that. But the `SYSTEMTIME` thing... you seem not to be initiating it at all. – Michał Górny Jul 30 '12 at 20:48
  • I am. You just can't see it because I didn't feel like posting all of the atrocious all at once. But yes it is: "SYSTEMTIME a;" – TriX Jul 30 '12 at 20:50
  • Well, if you're insist on using Windows-specific `SYSTEMTIME` thing, you're probably need to use [GetTimeFormat()](http://msdn.microsoft.com/en-us/library/windows/desktop/dd318130(v=vs.85).aspx) to convert the time into string. If you're fine with standard C/C++, [strftime()](http://www.cplusplus.com/reference/clibrary/ctime/strftime/) is probably simpler. – Michał Górny Jul 30 '12 at 20:54