Usenet.com

www.Usenet.com

Group Index

Comp Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Re: Having problems returning a char* from a function..need help...



Here is a working version now in case anyone wants to see..

Mark



Compile and ran under Visual C++ v6.0 ( VS 97 )....



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


#define MAX_TIME_LEN           6
#define MAX_FILE_NAME_LENGTH  12
#define FILE_NAME_EXT         ".log"

char cMonths[12][2] = { "JN", "FB", "MR", "AP", "MY", "JU",
                        "JL", "AG", "SP", "OC", "NV", "DC" };

/* provide a prototype for getAutoBITELogFileName() */
int getAutoBITELogFileName( char *fileName );



int main( )
{
  char *sAutoBITELogFileName;
  int iResult;

  sAutoBITELogFileName = malloc( MAX_FILE_NAME_LENGTH + 5 );
  _strset( sAutoBITELogFileName, 0 );
  
  iResult = getAutoBITELogFileName( sAutoBITELogFileName );


  printf( "lfn: '%s'\n\n", sAutoBITELogFileName );
  fflush( stdout );
  return 0;
}



int getAutoBITELogFileName( char *fileName )
{
  time_t        currentTime;
  struct tm*    daynow;
  int           iResult;
  char          cTemp[ MAX_TIME_LEN + 1 ];
  char          *tempFileName;

  /*sizeof *fileName is always 1, multiply by it anyway
   DOS file name is 8 dot 3 for a total of 12 characters plus
   a null terminating character. It will be defined later. */

  tempFileName = malloc( MAX_FILE_NAME_LENGTH + 1 );

  memset( tempFileName, 0, MAX_FILE_NAME_LENGTH + 1 );
  memset( cTemp, 0, MAX_TIME_LEN + 1 );
  
  if ( tempFileName == NULL ) {
     return 0;   /* Problem allocating memory */
  }

  /* get the current arithmetic calendar time */
  currentTime = time(NULL);

  /* Convert the current arithmetic calendar time into local
     time held in a structure of type tm. */
  daynow = localtime(&currentTime);

  strncpy( tempFileName, cMonths[daynow->tm_mon], 2 );

  /* MAX_TIME_LEN is 6 since the format of the file name is
     MONTH/DATE/HOUR/MIN which are all two character for a
     total of 8. So 8 minus two for the 6 with a null
     terminating character.*/
     
  iResult = strftime(cTemp, MAX_TIME_LEN + 1, "%d%H%M", daynow);

  /* Check to see if there was an error */
  if ( iResult == 0 ) {
    return 0;
  }

  strncat( tempFileName, cTemp, MAX_TIME_LEN );
  strncat( tempFileName, FILE_NAME_EXT, sizeof( FILE_NAME_EXT ) );
  strcpy( fileName, tempFileName );
 
  iResult = strlen( tempFileName );
  free( tempFileName );

  return iResult;

}




<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.