Solved

Text file name saved as current date and time?

Posted on 2007-04-02
4
274 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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 …
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 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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now