Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving the time into the name of the output file??? 1

Status
Not open for further replies.

newbieKing

Programmer
Jan 29, 2003
19
CA
Hi!
I can't figure out how to save the current OS date into the name of the output file. For example, if i run the program now, i want the output to be printed into a file called something like "Statistics_April4.txt".
This is what i've tried, but i keep on getting errors.
Please help!

#include <math.h>
#include <conio.h>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
using namespace std;

void main()
{
FILE *fopen(), *fp;

char tmpbuf[128];
string date = _strdate( tmpbuf );
string name = &quot;Statistics&quot;;
string filename = name + date;
fp = fopen( filename, &quot;w&quot;);

// i've also tried:
//ofstream fout(filename);
//fout << &quot;hahah&quot; << endl;
}
 
You had more problems!
1. you don't need FILE *fopen();
2. fopen does not accept string as first parameter;
3. The file name you wanted to the disk was &quot;Statistics 04/10/03&quot;; That name is invalid. Instead I replaced / with _.

#include <math.h>
#include <conio.h>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
using namespace std;

void main()
{
FILE *fp;
char tmpbuf[128],filename[150];
char *date = _strdate( tmpbuf );
char name[] = &quot;Statistics&quot;;
sprintf(filename,&quot;%s %s&quot;, name , date);
for(int i=0;i<=strlen(filename);i++)
if(filename=='/')filename='_';
fp = fopen(filename , &quot;w&quot;);
}

I hope I helped!
 
// date.cpp : Defines the entry point for the console application.
//
#include &quot;stdafx.h&quot;
#include <stdio.h>
#include <time.h>

const char* months[] = { &quot;January&quot;, &quot;Feruary&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;November&quot;, &quot;December&quot; };

void main()
{
char name[128];

time_t now;
tm* tmnow;

time(&now);
tmnow = localtime(&now);

sprintf(name, &quot;Statistics_%s_%02d.txt&quot;, months[tmnow->tm_mon], tmnow->tm_mday);

FILE* fp = fopen(name, &quot;w&quot;);
fprintf(fp, &quot;blabla\n&quot;);
fclose(fp);
}
 
Thank you for the help, but i have more problems...
I need to write my data into 3 output files while
the data is output with functions, (which are in the global scope).
The problems are:
1. I do not know how to use old C syntax and output to multiple files at once.
2. I do not know how to create the file name with the time included in the name outside of the main(). The compiler does not allow me to create the file name outside of the main().
Please help!

I want to replace the file name with one which includes the date

ofstream fout5(&quot;SchedulingData.txt&quot;, (ios_base::eek:ut | ios_base::app)); //--- main output file

ofstream fout6(&quot;SchedulingDataBackUp3.txt&quot;, (ios_base::eek:ut | ios_base::app)); //--- back up output file in case dynamic memory crashes

ofstream fout7(&quot;SchedulingParameters3.txt&quot;, (ios_base::eek:ut | ios_base::app)); //--- parameters recording
 
Code:
using std::string;
string filename=&quot;SchedulingData&quot;;
Date today;
today.getTodaysDate();//customize here
filename+=today.ConvertToString();//customize here
filename+=&quot;.txt&quot;;

ofstream fout5(filename.c_str(),(ios_base::out | ios_base::app)); //--- main output file
thats how you can make it make a filename based on the time.
the tricky part is to write a class that does that.
Fortunatly for something that trivial there are hundreds of free classes online. WR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top