Date format.

I am supposed to write a date into the file.

let's say for 1 jan 2012, I want to write a file as 01/01/2011, how can I do that???

Pls help..
Hi sanu

What sort of input do you have, a file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

void writeFile(int year, int month, int day) {
	std::ofstream out("date.txt");
	out << month << "/" << day << "/" << year << std::endl;
	out.flush();
	out.close();
}

int main() {
	int year, month, day;
	std::cout << "Enter year: ";
	std::cin >> year;
	std::cout << "Enter month: ";
	std::cin >> month;
	std::cout << "Enter day: ";
	std::cin >> day;

	writeFile(year, month, day);
}
@ TheIdeasMan: I read the date value from the file and stored in the vector. Now I want to pull the date (integer ) from the vector and write on the another file. But my criteria is if the value of day and month is < 10, the 0 Should be added before the integer value. I can write the function to do that, but I am looking for some simpler way whether any method exists already.. Thanks


@LimeOats: THanks for your comments but do that works while I must need to include before the month and day value less than 10.
EG. if the month is 1 , then the value written on the file should be 01 and so on.

Thanks
@spaggy: THanks a lot.. indeed a great help for me..

I did try before also but now i realise my mistake.. what i did was just put the setfill without any setw()....

Cheers!!!!
Topic archived. No new replies allowed.