06-20-2008 01:43 AM - edited 06-20-2008 01:53 AM
06-20-2008 03:03 AM
In my opinion both of your solutions are good. I can add a couple of alternatives to handle this item.
1. Use variants. Variant is a datatype that can hold several different data; CVI ActiveX library has some functions CA_VariantHasX to query if a variant contains a stated data type, so that your solution a. could be migrated as follows:
void WriteLabelValuePairToFile(int fileHdle, char *label, VARIANT value)
{
int data;
if (CA_VariantHasInt (&value)) {
CA_VariantGetInt (&value, &data);
FmtFile......(int)data;
}
.
.
.
}
The caller of this function is responsible of assigning the correct value to the variane using proper CA_VariantSetX function:
CA_VariantSetInt (&value, intVal);
2. Use variable parameters macros: here a function that writes a logfile taking a variable list of arguments as a parameter:
void History (int operation, int station, int *parms, ...)
// Activity log on file
{
int fH, error = 0;
char type, msg[256];
SYSTEMTIME lt;
va_list ap;
// Start variable params management
va_start (ap, parms);
// Open log file
fH = OpenFile ("Activity.log", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
if (fH == -1) {
error = GetFmtIOError ();
goto Error;
}
// Date / time of record
GetLocalTime (<);
sprintf (msg, "%02d/%02d/%04d %02d:%02d", lt.wDay, lt.wMonth, lt.wYear,
lt.wHour, lt.wMinute);
sprintf (msg, "%s,%d,%d", msg, station, operation);
// Variable parameters
while ((type = *parms++) != 0) {
switch (type) {
case INTPRM: sprintf (msg, "%s,%d", msg, va_arg (ap, int)); break;
case DBLPRM: sprintf (msg, "%s,%.1f", msg, va_arg (ap, double)); break;
case CHRPRM: sprintf (msg, "%s,%s", msg, va_arg (ap, char *)); break;
}
}
// Write to file and close
strcat (msg, "\n");
if (WriteFile (fH, msg, strlen(msg)) < 0) {
error = GetFmtIOError ();
goto Error;
}
Error:
CloseFile (fH);
// End variable parms management
va_end (ap);
if (error) {
// Conveniently handle errors
}
return;
}
The caller of this function must pass a variable lengh list of parameters ended with a null element:
parms[0] = INTPRM; parms[1] = 0; History (18, 1, parms, i);
06-20-2008 10:14 AM
06-22-2008 06:33 PM
Thanks to both!
it is data from an instrument that has to be saved in a format for easy and
consistent evaluation by external software rather than saving panel states.
I've made the final decision to use VARIANT as it seems the ideal data type for
that and allows me to later extend my function.
Thanks heaps for your advice!!