L
LongBow
Hello all,
From doing a google serach in the newsgroups I found out that a
string can't be returned from a function, but using a char* I should
be able to do it. I have spent most of the day trying to get this to
work, but been unable to solve my mistake. What the function should
return is a file name used for creating logs files. It will look
something like
JN122345.log
JN - Month
12 - Date
23 - Hour
45 - Min
Can anyone provide any assistance, please. Thanks!.
Mark
--------- source code ------------
#define MAX_TIME_LEN 6
char cMonths[12][2] = { "JN", "FB", "MR", "AP", "MY", "JU",
"JL", "AG", "SP", "OC", "NV", "DC" };
main( argc, argv )
int argc;
char *argv[];
{
char fileName[80];
*sAutoBITELogFileName = getAutoBITELogFileName( );
// Just seems to print only on character.
_settextposition( 1,1 );
printf( "lfn: '%s'", sAutoBITELogFileName );
}
char *getAutoBITELogFileName( )
{
/*----------------------------------------
%d Day (01-31)
%H Hour 24-hour style (00-23)
%M Minute (00-59)
------------------------------------------*/
time_t currentTime;
struct tm* daynow;
int iResult;
char cTemp[ MAX_TIME_LEN + 1 ];
char *fileName;
/*sizeof *fileName is always 1, multiply by it anyway*/
fileName = malloc( 13 * sizeof(*fileName) );
if ( fileName == NULL ) {
return NULL; // Problem allocating memory
}
currentTime = time(NULL); // get the current arithmetic calendar
time
// Convert the current arithmetic calendar time into local time held
in a
// structure of type tm.
daynow = localtime(¤tTime);
strncpy( fileName, cMonths[daynow->tm_mon], 2 );
iResult = strftime(cTemp, MAX_TIME_LEN + 1, "%d%H%M", daynow);
// Check to see if there was an error
if ( iResult == 0 ) {
return NULL;
}
strncat( fileName, cTemp, 6 );
strncat( fileName, ".log", 4 );
// Generally prints garbage, but some times can see the filename.
_settextposition( 25, 1 );
printf( "fn: %s", &fileName );
return (char *)fileName;
}
From doing a google serach in the newsgroups I found out that a
string can't be returned from a function, but using a char* I should
be able to do it. I have spent most of the day trying to get this to
work, but been unable to solve my mistake. What the function should
return is a file name used for creating logs files. It will look
something like
JN122345.log
JN - Month
12 - Date
23 - Hour
45 - Min
Can anyone provide any assistance, please. Thanks!.
Mark
--------- source code ------------
#define MAX_TIME_LEN 6
char cMonths[12][2] = { "JN", "FB", "MR", "AP", "MY", "JU",
"JL", "AG", "SP", "OC", "NV", "DC" };
main( argc, argv )
int argc;
char *argv[];
{
char fileName[80];
*sAutoBITELogFileName = getAutoBITELogFileName( );
// Just seems to print only on character.
_settextposition( 1,1 );
printf( "lfn: '%s'", sAutoBITELogFileName );
}
char *getAutoBITELogFileName( )
{
/*----------------------------------------
%d Day (01-31)
%H Hour 24-hour style (00-23)
%M Minute (00-59)
------------------------------------------*/
time_t currentTime;
struct tm* daynow;
int iResult;
char cTemp[ MAX_TIME_LEN + 1 ];
char *fileName;
/*sizeof *fileName is always 1, multiply by it anyway*/
fileName = malloc( 13 * sizeof(*fileName) );
if ( fileName == NULL ) {
return NULL; // Problem allocating memory
}
currentTime = time(NULL); // get the current arithmetic calendar
time
// Convert the current arithmetic calendar time into local time held
in a
// structure of type tm.
daynow = localtime(¤tTime);
strncpy( fileName, cMonths[daynow->tm_mon], 2 );
iResult = strftime(cTemp, MAX_TIME_LEN + 1, "%d%H%M", daynow);
// Check to see if there was an error
if ( iResult == 0 ) {
return NULL;
}
strncat( fileName, cTemp, 6 );
strncat( fileName, ".log", 4 );
// Generally prints garbage, but some times can see the filename.
_settextposition( 25, 1 );
printf( "fn: %s", &fileName );
return (char *)fileName;
}