MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: If and openwrite and openappend

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49208] Re: If and openwrite and openappend
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Wed, 7 Jul 2004 01:43:05 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 7/6/04 at 3:34 AM, camartin at snet.net wrote:

>I'm trying to write a piece of code to store strings in a file.  A
>very simplified example follows:

>Clear[t] Clear[nix] nix[x_String, y_String] :=
>t = {x, y}
>If[FileType["c:\\Mathematica\\dBaspap"] == None,
>stmp = OpenWrite["c:\\Mathematica\\dBaspap"], stmp =
>OpenAppend["c:\\Mathematica\\dBaspap"]]
>Write[stmp, t] Close[stmp]

>The function seems to work as I want if the file doesn't exist.  If
>it exists the code doesn't work.

>If I  the OpenAppend from the If statement things work fine.  But a
>subsequent use of nix fails because it refuses to open a stream.

Although, I don't see why your code doesn't work I don't see any need for If. The function OpenAppend will create a new file if one doesn't exist. Also, you don't the variable t since Write accepts an arbitrary number of expressions to write to a file. That is,

nix[strs___String]:=
  Module[{stmp=OpenAppend@"c:\\Mathematica\\dBaspap"},
    Write[stmp,strs];
    Close@stmp]
    
Should do what you want. Note by using the strs___String construct you can pass as many strings as you want to nix.
--
To reply via email subtract one hundred and four


  • Prev by Date: sorting polynomials by the degree of certain terms
  • Next by Date: Re: If and openwrite and openappend
  • Previous by thread: If and openwrite and openappend
  • Next by thread: Re: If and openwrite and openappend