M
marvind
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;
/***********************************************************************/
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;
/***********************************************************************/