Re: padding a file
- From: roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson)
- Date: Thu, 28 Jul 2005 19:03:30 +0000 (UTC)
In article <1122575672.574568.22060@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
marvind <marvind434@xxxxxxxxx> wrote:
>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 .
>fprintf(fp, "\n");
--
Ceci, ce n'est pas une idée.
.
- Follow-Ups:
- Re: padding a file
- From: marvind
- Re: padding a file
- References:
- padding a file
- From: marvind
- padding a file
- Prev by Date: Re: strange pointer behavior
- Next by Date: Re: padding a file
- Previous by thread: padding a file
- Next by thread: Re: padding a file
- Index(es):
Relevant Pages
|