Re: padding a file



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.
.



Relevant Pages

  • Re: smtplib - missing message
    ... > When I look at the received mail, ... > server.sendmail(fromClause, toClause, msg) ... Note the double newline to mark the end of the header fields. ...
    (comp.lang.python)
  • Re: FAQ 9.20 How do I send mail?
    ... timdooling wrote: ... There should be a newline at the end of each header "line". ... You should check the return value from closeon piped filehandles. ...
    (comp.lang.perl.misc)
  • Re: csv module
    ... >>> import csv ... I found the pb: I add a newline '\r\n' to ... separate the header from the records... ...
    (comp.lang.python)
  • padding a file
    ... I want to write a header line and some logs to a file. ... code below is correct (since I am using fwrite). ... * Also must append a newline to padding before ...
    (comp.lang.c)