padding a file



Hello,

I want to write a header line and some logs to a file. I cannot
construct the header line until I have seen the last log line. I
decided to pad the beginning of the file and then write the logs (all
text). After I have written out all the log lines, I seek to the
beginning of the file and write the header line. I want to know if the
code below is correct (since I am using fwrite).

I am not worried about wastage of disk space because I delete the file
once I have processed it.

Thanks

/************* CODE
***************************************************/
FILE *fp = NULL;

/** FUNCTION: BeginLog **/

/* open temporary file in text mode for writing */
fp = fopen("logs.tmp", "wt");
if (NULL == fp)
{
return -1;
}

/* padding - reasonable assumption that header is < 4000 bytes */
char szPadding[4096];
memset(szPadding, 0, 4096);
if (1 != fwrite(szPadding, sizeof(szPadding), 1, fp))
{
return -1;
}
/*
* fwrite will not translate '\n',want to read it as text
* Also must append a newline to padding before
* writing first log line in order to use fgets
*/
fprintf(fp, "\n");

/********************/
/** FUNCTION: EndLog **/

if (NULL != fp)
{
fseek(fp, 0, SEEK_SET);
/* header line passed in or global */
fprintf(fp, "%s\n", headerLine);
fclose(fp);
fp = NULL;
}
return;

/**** FUNCTION: read ***/

FILE *fp = fopen("logs.tmp", "rt");
char line[4096];
while (NULL != fgets(line, 4096, fp))
{
printf("%s", line);
}
return 0;

/***********************************************************************/

.



Relevant Pages

  • Re: Writing binary data to instrument with header
    ... Using fwrite wouldn't work because the header is ... The array is preceded by a four-byte header. ... assuming the posted description is consistent w/ the actual wording. ...
    (comp.soft-sys.matlab)
  • Re: Writing binary data to instrument with header
    ... Using fwrite wouldn't work because the header is ... The array is preceded by a four-byte header. ... assuming the posted description is consistent w/ the actual wording. ...
    (comp.soft-sys.matlab)
  • Re: Printer friendly ASP.Net
    ... body {padding: 20px;} ... You can reduce the margin and padding specifically for print, as in my example, but the browser also adds margin outside that area, as well as the header and footer. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Help needed with minor CSS layout problems
    ... and setting margin and padding properties for "body". ... I am still trying to find a way to make the white text in the header ... The non-positioned image on the left sets the min height of the containing DIV 'header', by setting vertical-align: middle to that element means that the follow text will vertical aligned with that image regardless of the size of the text and with respect to the DIV 'header' unless a wrap occurs. ...
    (alt.html)
  • Re: Header Background
    ... I know that if I apply the background color to the header it will ... Are you talking about the way the span's padding and border overflow the ... I would advise don't decorate inline boxes in this way. ... if I use float I will need to clear the float after it the header ...
    (comp.infosystems.www.authoring.stylesheets)