Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I create file name from date/time?

Status
Not open for further replies.

TimRHSD

Programmer
Apr 18, 2002
35
US
Hi,

I am trying to create a file name with an extention of YYYMMDD_HHMM. e.g. Employee.20020916_1336

Currently, when we run a global send the error messages are trapped in a file called employee.msg, which is fine unless we find we goofed on an employee and just process that one record afterward. The big file we created earlier is now wiped out by the one record. I want to keep a record for all changes/errors.

Thanks for any suggestions. It doesn't sound that hard, but I don't know C well enough to do it.

Sincerely,

Tim
 
Tim:

Nothing that hasn't been done before. get the system time, and set up a pointer-to-a-tm structure.

Works on my Red Hat Linux 7.1 and Solaris 7.

Let me know if you have any questions.

Ed

#include <stdio.h>
#include <sys/types.h>
#include <time.h>

void main(int argc, char **argv)
{
time_t tloc; /* generally time_t is a long */
struct tm *lc_time;
char filename[40];

time(&tloc); /* get time in seconds since EPOCH */
printf(&quot;time in seconds since EPOCH is %ld\n&quot;, tloc);

lc_time=localtime(&tloc); /* convert seconds to date structure */

sprintf(filename, &quot;%04d%02d%02d_%02d%02d&quot;, lc_time->tm_year + 1900, lc_time->tm_mon+1,
lc_time->tm_mday, lc_time->tm_hour, lc_time->tm_min);

printf(&quot;%s\n&quot;, filename);

}
 
That worked in a program by itself. Now all I have to do is get it to work in the library routine. I keep getting a segmentation error (core dump). Below is the section I am trying to update.

Thanks, Tim

/* INCLUDES */

#include &quot;first.h&quot;
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include &quot;comdef.h&quot;

/* DEFINES */


/* SCCS ID */

static char *_sccs_id=&quot;@(#)&quot;;
static char GlobtcId[]=&quot;@(#)msg.c 2.2 08/25/95 21:31:29&quot;;

/* Prototypes */

void popint(int *);
void popvchar(char *,int);
void retquote(char *);
void retint(int);
static FILE *pFile;

int openglobtcfile(cNumParms)
{
char szScrName[13];
char szFileName[20];

popvchar(szScrName,12);
Clip(szScrName);
if (strlen(szScrName) == 0)
{
retint(1);
return 1;
}
strcpy(szFileName,szScrName);
/* trying to get rid of '.msg' and replace it with timestamp */
strcat(szFileName,&quot;.msg&quot;);
pFile = fopen(szFileName,&quot;w&quot;);
if (pFile == (FILE *) NULL)
{
retint(2);
return 1;
}

retint(0);
return 1;
}
 
Tim:

Based on your function names, it looks like you're writing Informix embedded &quot;C&quot; functions, ESQL, or &quot;C&quot; function callable by Informix 4GL. At any rate, it looks like you're passing variables on the stack, and returning vars on the stack.

Although I don't recognize several of your functions, i.e. popvchar, clip, I have some experience with writing callable 4GL programs. I've posted a FAQ over in the Informix Online forum:

faq179-2007

I have an example function which is analogous to what you are doing - passing a file name and opening it. Mine is similar to yours, but, of course, different. (I use function popchar instead of popvchar).

I think your core dump is probably a stack handling problem. One potential problem I see is this:

int openglobtcfile(cNumParms)
{
char szScrName[13];
char szFileName[20];

Doesn't cNumparms have to be defined? Such as:

int openglobtcfile(int cNumParms)

One other suggestion: Why not pass the whole filename, YYYYMMDD_hhss.msg, to openglobtcfile, and discard appending msg in the function?

Let me know if you have any other questions,

Ed
 
Ed,
You are correct. This is a function called by 4gl. Sorry, I didn't have the whole thing pasted in. Below is the top of that program. Oops. The file name that is currently being built is &quot;BENEFITS.msg&quot; where BENEFITS would be the name of the field. All screens write this file to the same directory so I need to know from which screen the file came. That is why I want something like BENEFITS.20020913_1230.

HTH.

Thanks,

Tim

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top