Solved

Text file name saved as current date and time?

Posted on 2007-04-02
4
288 Views
Last Modified: 2013-12-14
I have a program with needs to contain the system's date and time as a text file name for a flat file that the C++ program creates (ex:  20070402134525.txt)

I've already got the program pulling the system date and time (see below.)  I just need to know how I can get that information in to the file name.

char buffer[SIZE];
time_t curtime;
struct tm *loctime;
     
// Get the current time.
curtime = time (NULL);
     
// Convert it to local time representation.
loctime = localtime (&curtime);
         
 //Date and time format
 strftime (buffer, SIZE, "%Y%m%d%H%M%S", loctime);
0
Comment
Question by:jgoss1
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18838707
You can create a file with that name like

#include <stdio.h>

FILE* p = fopen(buffer,"w"); // file name is in 'buffer'

or

#include <fstream>
using namespace std;

ofstream os(buffer); // file name is in 'buffer'

Which method are you using to open the file?
0
 

Author Comment

by:jgoss1
ID: 18838864
I'm using:  #include <stdio.h>

I've got the file going to a testing file called "651_GBI.txt."  Once I put it in to production, they'll be sending the flat file about every thrity minutes.  So, I'll need it to save the flat file as "new" each time and not write over the previous copy.

Here's where I'm sending my test file.
FILE *file = fopen("y:\\incoming\\oss\\down\\651_GBI.txt", "r");
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 18838891
Then make that like

char buffer[SIZE];
time_t curtime;
struct tm *loctime;
     
// Get the current time.
curtime = time (NULL);
     
// Convert it to local time representation.
loctime = localtime (&curtime);
         
 //Date and time format
 strftime (buffer, SIZE, "%Y%m%d%H%M%S", loctime);

char path[MAX_PATH];

sprintf(path,"y:\\incoming\\oss\\down\\%s.txt", buffer);

FILE *file = fopen(path, "w"); // 'w' for write access
0
 

Author Comment

by:jgoss1
ID: 18838956
Worked like a dream!  I can't believe I'm spent the last hour banging my head on my keyboard over this one...that was too easy! Thanks jkr!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question