Unsure how to write date/time to txt file.

Hi, as the forum says, I'm a beginner.
I'm used to higer level languages where things are quite a lot simpler.
What I've been trying to do and what I need help with is how I can incorporate the the date/time into a string I write to a file.
I've already got a basic setup which allows me to write to a file but I cannot, for the life of me, work out all this time malarky. It seems like there's a thousand different ways of doing it and any similar topics I've read prior to now have been filled with what seemed like heavily conflicting advice.
If it influences the solution at all I'm using Visual Studio 2013 and here is the basic code I have right now.

I'm not asking for you to thow code at me but I'd love some advice that gets me moving in the right direction. Thankyou in advance for taking the time to read this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

extern "C" {
	__declspec (dllexport) void __stdcall RVExtension(char *output, int outputSize, const char *function);
}

void __stdcall RVExtension(char *output, int outputSize, const char *function) {
	ofstream myfile;
	myfile.open("salog.txt", ios::app);
	myfile << "\n";
	myfile << function;
	myfile.close();
}
Last edited on
Hiya,
1. Did you want to do it in terms of writing a DLL (which it looks like you're doing here), or do you want to do it in a 'normal' c++ program? If you are a beginner Im not sure why you'd want to go down a DLL writing route.
2. I see no date information in that code segment. If you want to write time/date info into a text file can't you do it in any format you want?
// http://www.cplusplus.com/reference/ctime/strftime/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>      /* puts */
#include <time.h>       /* time_t, struct tm, time, localtime, strftime */

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  char st [80];
	
  time (&rawtime);
  timeinfo = localtime (&rawtime);

  strftime (st,80,"Date: %y-%m-%d  Time: %I:%M%p",timeinfo);
  
  puts (st);
  return 0;
}
Hi guys, thanks for answering.

@mutexe,
I forgot to mention but I'm creating this as part of an addon for a game, ArmA 2. So it must be a DLL. There was no date related code in the segment I showed because I failed to get it working but thought I would still provide a view of how the basic code is laid out, in case it helped with the solution.

@anup30,
I've seen that snippet before and when I implemented it I got notifications/errors telling me that 'localtime' is a depricated function. I thought I would try it regardless and it simply did not create any value. I will revisit it, however, maybe I just did something wrong, which wouldn't surprise me :')

Thank you both for your answers.

EDIT: Thankyou anup30, after some playing around that code snippet you provided did help me succeed after a bit of fiddling :) I appreciate your help a lot!
Last edited on
Topic archived. No new replies allowed.