Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #59880

Re: How to use a temporary file in a portable way

From Nobody <nobody@nowhere.invalid>
Newsgroups comp.lang.c
Subject Re: How to use a temporary file in a portable way
Date 2015-04-10 14:48 +0100
Organization A noiseless patient Spider
Message-ID <pan.2015.04.10.13.48.37.131000@nowhere.invalid> (permalink)
References <mg527d$f4p$1@dont-email.me>

Show all headers | View raw


On Thu, 09 Apr 2015 07:22:22 +0200, pozz wrote:

> I understand the more portable and secure method to change the content of
> a file is through creating a temporary file.
> 
> For example, when I need to change a CSV file, a simple configuation file
> (typical in Unix systems), a INI file (typical on Windows systems) and
> many other proprietary and standard format.
> 
>    open/create temporary file
>    put the new content on temporary file close temporary file
>    remove original file
>    rename temporary file with original filename

On Unix, you shouldn't remove the original file, but simply rename() the
new file over it. That avoids having an interval where the target file
doesn't exist. On Windows, the separate remove step is necessary, as
rename() fails if the target exists.

> Should I create a temporary file in the executable directory?  Or in the
> original file directory?  Or in a system-dependent (non portable)
> temporary directory?

The temporary file should be on the same filesystem (partition) as the
target, otherwise rename() will fail. Placing it in the same directory
ensures that this is the case.

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

How to use a temporary file in a portable way pozz <pozzugno@gmail.com> - 2015-04-09 07:22 +0200
  Re: How to use a temporary file in a portable way Robert Wessel <robertwessel2@yahoo.com> - 2015-04-09 02:15 -0500
  Re: How to use a temporary file in a portable way Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-09 11:00 +0100
  Re: How to use a temporary file in a portable way James Kuyper <jameskuyper@verizon.net> - 2015-04-09 08:42 -0400
  Re: How to use a temporary file in a portable way Greg Martin <greg.at.softsprocket@dot.com> - 2015-04-09 15:47 +0000
    Re: How to use a temporary file in a portable way Michael Angelo Ravera <maravera@prodigy.net> - 2015-04-09 14:52 -0700
  Re: How to use a temporary file in a portable way Richard Damon <Richard@Damon-Family.org> - 2015-04-09 23:43 -0400
    Re: How to use a temporary file in a portable way Keith Thompson <kst-u@mib.org> - 2015-04-10 08:29 -0700
      Re: How to use a temporary file in a portable way Richard Damon <Richard@Damon-Family.org> - 2015-04-10 22:31 -0400
  Re: How to use a temporary file in a portable way Nobody <nobody@nowhere.invalid> - 2015-04-10 14:48 +0100

csiph-web