padding a file

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;

/***********************************************************************/
 
W

Walter Roberson

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).
/* open temporary file in text mode for writing */
fp = fopen("logs.tmp", "wt");

Noted: text mode rather than binary.
/* 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

That comment indicates to us that you are concerned about portability.
However:

a) In C89, conforming implementations need not support more than 254
text characters before the newline;
b) In C89, what is input in text mode need not compare equal to
what was output unless the output was restricted to printable characters,
horizontal space, and newlines -- so in text mode writing 0's
is not certain to write any particular size of padding
c) Your comment is incorrect: fwrite() in text mode *will* translate \n .
 
M

marvind

Hello again,

I wanted to make sure that I was not committing any other mistake
related to this topic.

Based on your input, I decided to use binary mode to read from and
write to the log file because:
1. The log lines have unprintable ascii as field delimiter.
2. Due to the way our code is structured, I may have to pass the
position returned by ftell and close the file. The file is reopened in
binary mode and then the code fseeks to this position and starts
reading from this position (I cannot change this). To avoid a mismatch
here, I think I will use binary mode consistently to read the file.
3. I will continue to use fprintf to write to the file and not fwrite
of structures to avoid portability issues when dealing with int,
floats, etc.
4. As before, I will use a character array to store the padding and
write that out.

Please let me know if I am committing a mistake.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top