Date as string

I am trying to get the current date and use that as a string for the filename. So it would be like .... 20120126 Reconciliation.csv....but i am having problem setting up the string. Please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <string.h>


using namespace std;
class date
{
	int month;
	int day;
	int year;
public:
	date(int month =1, int day=1, int year =2001)
	{
		date::month = month;
		date::day = day;
		date::year = year;
	};
void showDate();
~date(){}
};

void date::showDate()
{
	cout << year << month << day << "Reconcile.csv" <<endl;
};

int main () 
{
	int day,month,year;
	date newDate(month,day,year);
	newDate.showDate();
	
	system("PAUSE");
  return 0;
}
cout doesn't have anything to do with files. You're printing a string literal that semantically could be a filename, but as far as the compiler is concerned it's no different than "ABCLOLZ".

Anyhoo, save your filename as a string and concatenate it with the necessary parts.
would making the following changes bring me to the right direction?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void date::showDate()
{
	cin >> year >> month >> day;
};

int main () 
{
	int year,month,day;
	string dates;
	cout << year << month << day;
	string sString = dates + "reconciliation.csv";
	ifstream filename(sString);
	
	system("PAUSE");
  return 0;
}
Yes, but also no, because "dates" is uninitialized. I don't think you understand what "cout" does, but in any case: it has nothing to do with string manipulation.

There are some major mistakes in your code. For one thing: a function definition does not end with ";" (line 4). Secondly, showDate() shouldn't read; the naming suggest that it prints. Thirdly, just because you have a read function doesn't mean your data is initialized, because you never call your reading function.

An easy way to "make" strings is by using the function described here: http://www.cplusplus.com/reference/string/string/operator+=/

It adds a string-ish type to the end of a string. As a result, you can do this:
1
2
3
4
5
6
char part1[] = "Wo";
char part2[] = "rld";
string fullString("Hello ");
fullString += part1;
fullString += part2;
cout << fullString; // Will print "Lord Bobbington". 

Ints can be converted to strings using itoa(): http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

Alternatively, you can use stringstreams. Very easy to manipulate them!
To get the date and time, a useful tool can be found in <ctime>.

http://cplusplus.com/reference/clibrary/ctime/asctime/

1
2
3
4
5
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );


Now you have a structure called timeinfo which contains the time data you need.
Now lets feed it into your class:

1
2
3
4
5
  int day = timeinfo->tm_mday;
  int month = timeinfo->tm_mon+1;
  int year = timeinfo->tm_year+1900;

  date newDate(month,day,year);


Now you probably want to be able to extract a string in the format that you wanted like so:
string DateStamp = newDate.str();

To do this, lets create newDate.str(). This is what my version of your class would look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class date
{
public:
  date::date(int m, int d, int y) : _d(d), _m(m), _y(y) {} // CTOR with initialization lists
  std::string str()
  {
    std::stringstream iss; //We'll use a stringstream to concatinate the dates
    iss << std::setw(4) << std::setfill('0') << _y; //setw() and setfill('0') ensure leading zeros are there.
    iss << std::setw(2) << std::setfill('0') << _m << _d;
    return iss.str(); //Now we just return the string present in iss.
  }
private:
  int _d, _m, _y;
}


The includes you need are:
1
2
3
4
#include <ctime>
#include <string>
#include <sstream>
#include <iomanip> 


Now that you have the string, I think you can open your text file!
Last edited on
Add std::strftime() http://cplusplus.com/reference/clibrary/ctime/strftime/
to std::time() and std::localtime() and <ctime> would do all the heavy lifting.

1
2
3
4
5
6
7
8
9
std::time_t rawtime ;
std::time ( &rawtime ) ;
const std::tm* timeinfo = std::localtime ( &rawtime ) ;

char yyyymmdd[16] ;
std::strftime( yyyymmdd, sizeof(yyyymmdd), "%Y%m%d", timeinfo ) ;

const std::string path_suffix = "Reconciliation.csv" ;
const std::string file_name = yyyymmdd +  path_suffix ;

Thanks guys for the input however I ran into some problems.
JLBorges- "warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\time.inl(112) : see declaration of 'localtime'" is the message I got. It says build is successful but I dont see it creating a new file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <ctime>
#include <string>
#include <sstream>
#include <iomanip>
#include <stdio.h>
#include <time.h>
#include <fstream>


int main ()
{
std::time_t rawtime ;
std::time ( &rawtime ) ;
const std::tm* timeinfo = std::localtime ( &rawtime ) ;

char yyyymmdd[16] ;
std::strftime( yyyymmdd, sizeof(yyyymmdd), "%Y%m%d", timeinfo ) ;

const std::string path_suffix = "Reconciliation.csv" ;
const std::string file_name = yyyymmdd +  path_suffix ;

  std::ifstream inData(file_name);

  inData.close();


  
return 0;
}



Stewbond- Below is the error i received: error C2628: 'date' followed by 'int' is illegal (did you forget a ';'?)
error C3874: return type of 'main' should be 'int' instead of 'date'
error C2664: 'date::date(const date &)' : cannot convert parameter 1 from 'int' to 'const date &'
1> Reason: cannot convert from 'int' to 'const date'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <ctime>
#include <string>
#include <sstream>
#include <iomanip>
#include <fstream>


class date
{
public:
  date::date(int m, int d, int y) : _d(d), _m(m), _y(y) {} // CTOR with initialization lists
  std::string str()
  {
    std::stringstream iss; //We'll use a stringstream to concatinate the dates
    iss << std::setw(4) << std::setfill('0') << _y; //setw() and setfill('0') ensure leading zeros are there.
    iss << std::setw(2) << std::setfill('0') << _m << _d;
    return iss.str(); //Now we just return the string present in iss.
  }
private:
  int _d, _m, _y;
}
int main()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );

  int day = timeinfo->tm_mday;
  int month = timeinfo->tm_mon+1;
  int year = timeinfo->tm_year+1900;

  date newDate(month,day,year);
  std::string DateStamp = newDate.str();
  std::string filename = DateStamp + "reconciliation" + ".csv";

  std::ifstream inData(filename);

  inData.close();
  return 0;
  }



> 'localtime': This function or variable may be unsafe. Consider using localtime_s instead.

Any function, including the non-standard localtime_s(), would be unsafe if it is used in an unsafe manner. std::localtime() is standard C++; localtime_s() is not.


> I dont see it creating a new file.

The program didn't create a new file because nowhere in the program have you asked for a new file to be created.
Great it works!Thanks.
Last edited on
Topic archived. No new replies allowed.