Hello,
I do not understand what the difference is between "string" and "decimal". Maybe you can elaborate for other readers also.
However I sometimes have to flush many ASCII formatted values to a file. Opening and closing the file for each value would increase overhead. So I use a kind of diskbuffer. When I use this diskbuffer with sprintf function, I have full control of the ASCII format for output.
// first select (global variable) filename and create file for writing text
fptr = fopen(ExportFileName, "w+t");
if (fptr != NULL)
{
sprintf(LongReportString, "ExportTool in MyProgramme.exe\n");
fputs(LongReportString, fptr);
fflush(fptr);
fclose(fptr);
}
// subsequent send data to disk buffer (see source below)
for (n = 0, n<10000, n++)
{
sprintf(LongReportString, "index: %6d , value: %8.3lf\n", n, mydoublevalue)
WriteToDiskBuffer(0, LongReportString);
}
// finish with flushing after last data set
WriteToDiskBuffer(1, NULL);
Succes, Jos
An example of my source for the diskbuffer:
#define MaxLengthReportString 80
#define MaxReportBufferStrings 10000
// option 1 at global level if used often:
// this option 1 is used in subsequent functions
static char ExportFileName[MAX_PATHNAME_LEN];
static char ReportDiskBuffer[((MaxReportBufferStrings+1)*(MaxLengthReportString+3))] = {0};
// option 2 at function level if used once:
long bufsize;
char * ReportDiskBuffer;
static char ExportFileName[MAX_PATHNAME_LEN];
bufsize = (MaxReportBufferStrings+1)*(MaxLengthReportString+3);
ReportDiskBuffer = (char *)calloc(bufsize, sizeof(char))
... ... ...
free (ReportDiskBuffer)
/***************************************************************************/
/* Function : Write string to - already existing - file */
/* In : FileName, pointer to string */
/* Out : Nothing */
/* Description: Appends to FileName. Writes ASCII string */
/***************************************************************************/
void FileAppendString(char *FileName, char *AppendString)
{
FILE *fptr;
if ((fptr = fopen( FileName, "a+t" )) == NULL )
return; /* or error handling */
fputs(AppendString, fptr);
fflush(fptr);
fclose(fptr);
}
/***************************************************************************/
/* Function : Write string to output buffer */
/* In : option to force output, pointer to string */
/* Out : Nothing */
/* Description: Appends character string to end of null-terminated buffer */
/* If forced or buffer-full, stream buffer to file */
/***************************************************************************/
void WriteToDiskBuffer(int Forced, char *Data)
{
int StringLength;
int FlushNow = 0;
if (Data != NULL)
strcat(ReportDiskBuffer, Data);
StringLength = strlen(ReportDiskBuffer);
if ((Forced > 0) || (StringLength > (MaxReportBufferStrings * MaxLengthReportString))) FlushNow = 1;
if (FlushNow > 0)
{
if (StringLength > 0)
{
FileAppendString(ExportFileName, ReportDiskBuffer);
ReportDiskBuffer[0] = '\0';
}
}
}
void FlushDiskBuffer(void)
{
WriteToDiskBuffer(1,NULL);
}