Solved

Text file name saved as current date and time?

Posted on 2007-04-02
4
293 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

749 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