?
Solved

Text file name saved as current date and time?

Posted on 2007-04-02
4
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

752 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