Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Text file name saved as current date and time?

Posted on 2007-04-02
4
Medium Priority
?
303 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

604 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