mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bernd Petrovitsch <bernd@firmix.at>
To: Adam Turk <bofh1234@hotmail.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: recommended programming practices for writing (was Linux 2.6.29)
Date: Thu, 26 Mar 2009 20:34:03 +0100	[thread overview]
Message-ID: <1238096043.13457.4.camel@spike.firmix.at> (raw)
In-Reply-To: <COL112-W81AF4A8C222B9941D8295FC8910@phx.gbl>

On Thu, 2009-03-26 at 15:03 -0400, Adam Turk wrote:
> I have been reading the Linux 2.6.29 thread with interest.  I have
> written several (10 or so) C programs that write large amounts of data
> (between 1 and 2 GB file sizes are common).  A snippet of code looks
> like this:
> 
> if((fptr = fopen(outfilename,"w")) == NULL) {
>   printf("File %s could not be created\n", outfilename);
> }
> else {
>   fprintf(fptr,"%s\n",datablock);
>   while(!writeouput(datablock,amount,tax)) {
>     getnext(dtablock)
>     fprintf(fptr,"%s\n",datablock);
>   }
>   fclose(fptr);
> }
> 
> I learned C about 15 years ago and there was no mention of a fsync.
> My C book doesn't mention fsync either.  Granted I have written only

Probably because fsync() is a system call (and not a standard C lib
function).

[...]
> From what Linus posted about git and checking the return from fclose I
> think I going to start doing that.  I also think I am going to start
> checking the return from fprintf and maybe write to a /tmp/file and
> then rename it.

For a really robust app it's probably not wrong. At least one gets an
error (e.g. "disk full") immediately and not only at fclose() time after
(trying to) write 2GB data.

> So is there a C fsync that I should add before my fclose?

"fflush(fptr);" flushes all of the buffers (managed by the C-lib) of
fptr and also delivers an error.
fsync() is to flush the in-kernel cached pages of that file.

> What is the proper way to write to files?

Basically just as you do above.

	Bernd
-- 
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services



  reply	other threads:[~2009-03-26 19:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-26 19:03 Adam Turk
2009-03-26 19:34 ` Bernd Petrovitsch [this message]
2009-03-26 22:33   ` Adam Turk
2009-03-26 19:35 ` Leon Woestenberg
2009-03-26 22:32   ` Adam Turk
2009-03-26 22:40 ` J.A. Magallón

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1238096043.13457.4.camel@spike.firmix.at \
    --to=bernd@firmix.at \
    --cc=bofh1234@hotmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®