1. how to append at the end of file ?
- Posted by "Gottwald, IT-IS T500, Fa. Compaq, DA" <T.Gottwald at DEUTSCHEPOST. Dec 12, 2000
- 332 views
------_=_NextPart_000_01C0642C.BECB3160 charset="iso-8859-1" If I believe in the Documentation .. its easy: "Output to a file opened for append will start at the end of file. " Let's try this under Win2k: global function t_appndf(sequence fn,object t) atom fhw,r,f fhw = open(fn,"a") if fhw=-1 then r=0 else r=1 puts(fhw,t) end if return r end function So now I call it: ok=t_appndf(Logfile,"startet\n") ok=t_appndf(Logfile,"ended\n") Now lets look at the resulting Logfile: " ended startet " Oh no - it "appended things" at the beginning of the file ! Whats my mistake here ? PS: Sent a short copy of the mailto:atp at c10.de thanks --Theo Gottwald http://www.theogott.de mailto:atg at theogott.de ------_=_NextPart_000_01C0642C.BECB3160 name="Gottwald, IT-IS T500, Fa. Compaq, DA.vcf"
2. Re: how to append at the end of file ?
- Posted by Rolf Schroeder <rolf.schroeder at DESY.DE> Dec 12, 2000
- 342 views
"Gottwald, IT-IS T500, Fa. Compaq, DA" wrote: > > If I believe in the Documentation .. its easy: > "Output to a file opened for append will start at the end of file. " > Let's try this under Win2k: > > global function t_appndf(sequence fn,object t) > atom fhw,r,f > fhw = open(fn,"a") > if fhw=-1 then r=0 else r=1 > puts(fhw,t) > end if > return r > end function > > So now I call it: > > ok=t_appndf(Logfile,"startet\n") > ok=t_appndf(Logfile,"ended\n") > > Now lets look at the resulting Logfile: > " > ended > startet > " > Oh no - it "appended things" at the beginning of the file ! > Whats my mistake here ? > > PS: Sent a short copy of the mailto:atp at c10.de thanks > > --Theo Gottwald > http://www.theogott.de > mailto:atg at theogott.de > > ------------------------------------------------------------------------ > > Gottwald, IT-IS T500, Fa. Compaq, DA.vcfName: Gottwald, IT-IS T500, Fa. > Compaq, DA.vcf > Type: VCard (text/x-vcard) Hi Gottwald, I changed a little bit your code...: --------------------------------------------------- global function t_appndf(sequence fn,object t) atom fhw, r fhw = open(fn,"a") if fhw=-1 then -- open fails... r=0 else r=1 puts(fhw,t) end if close(fhw) return r end function --------------------------------------------------- -- So now I call it: integer ok ok=t_appndf("Logfile","startet\n") ok=t_appndf("Logfile","ended\n") --------------------------------------------------- Have a nice day, Rolf
3. Re: how to append at the end of file ?
- Posted by Dan B Moyer <DANMOYER at PRODIGY.NET> Dec 12, 2000
- 349 views
Rolf, I was sure that your new code was wrong, when I saw that you closed the logfile without regard for whether it had passed or failed the open test, because every time I have used an open file to test for the existence of a file, if I closed for a file which didn't exist, I got an error. So, I read the fine manual :) , and discovered that for append or write, a file *will* be opened if there was none, so there should (probably?) never be any reason the open would ever fail, and so closing it should never be a problem. But with open for read or "update" (reading and then writing?), if you can't open a file because it doesn't exist, then telling it to close would generate an error. But even though it works, if you're going to put a test for open file in, shouldn't you put the close inside the "open succeeds" portion of the test, rather than outside both portions of that test, if for no other reason than to remind yourself that it must be there for open read? I notice Ad did it the same as you did, so I'm probably wrong, but I'll ask anyway. Dan Moyer ----- Original Message ----- From: "Rolf Schroeder" <rolf.schroeder at DESY.DE> To: <EUPHORIA at LISTSERV.MUOHIO.EDU> Sent: Tuesday, December 12, 2000 4:23 AM Subject: Re: how to append at the end of file ? > "Gottwald, IT-IS T500, Fa. Compaq, DA" wrote: > > > > If I believe in the Documentation .. its easy: > > "Output to a file opened for append will start at the end of file. " > > Let's try this under Win2k: > > > > global function t_appndf(sequence fn,object t) > > atom fhw,r,f > > fhw = open(fn,"a") > > if fhw=-1 then r=0 else r=1 > > puts(fhw,t) > > end if > > return r > > end function > > > > So now I call it: > > > > ok=t_appndf(Logfile,"startet\n") > > ok=t_appndf(Logfile,"ended\n") > > > > Now lets look at the resulting Logfile: > > " > > ended > > startet > > " > > Oh no - it "appended things" at the beginning of the file ! > > Whats my mistake here ? > > > > PS: Sent a short copy of the mailto:atp at c10.de thanks > > > > --Theo Gottwald > > http://www.theogott.de > > mailto:atg at theogott.de > > > ------------------------------------------------------------------------ > > > > Gottwald, IT-IS T500, Fa. Compaq, DA.vcfName: Gottwald, IT-IS T500, Fa. Compaq, DA.vcf > > Type: VCard (text/x-vcard) > > Hi Gottwald, > > I changed a little bit your code...: > --------------------------------------------------- > global function t_appndf(sequence fn,object t) > > atom fhw, r > > fhw = open(fn,"a") > if fhw=-1 then -- open fails... > r=0 > else r=1 > puts(fhw,t) > end if > close(fhw) > return r > end function > --------------------------------------------------- > -- So now I call it: > > integer ok > > ok=t_appndf("Logfile","startet\n") > ok=t_appndf("Logfile","ended\n") > --------------------------------------------------- > > Have a nice day, Rolf
4. Re: how to append at the end of file ?
- Posted by jiri babor <jbabor at PARADISE.NET.NZ> Dec 13, 2000
- 336 views
Theo, Your mistake was *not closing* the file before reopening it again with the second call of your function. Unfortunately, your function does not save the file handle. I have slightly modified your function to return the handle and to allow you to close the file. jiri -- test -------------------------------------------------------------- include file.e constant Logfile = "test.dat" integer ok global function t_appndf(sequence fn,object t) atom fhw fhw = open(fn,"a") if fhw = -1 then puts(1, "Error: couldn't open file" & fn & "!\n") abort(1) else puts(fhw,t) end if return fhw end function -- So now you can call it: ok = t_appndf(Logfile,"startet\n") close(ok) -- And again: ok = t_appndf(Logfile,"ended\n") close(ok) ----- Original Message ----- From: "Gottwald, IT-IS T500, Fa. Compaq, DA" <T.Gottwald at DEUTSCHEPOST.DE> To: <EUPHORIA at LISTSERV.MUOHIO.EDU> Sent: Wednesday, December 13, 2000 12:14 AM Subject: how to append at the end of file ? > If I believe in the Documentation .. its easy: > "Output to a file opened for append will start at the end of file. " > Let's try this under Win2k: > > global function t_appndf(sequence fn,object t) > atom fhw,r,f > fhw = open(fn,"a") > if fhw=-1 then r=0 else r=1 > puts(fhw,t) > end if > return r > end function > > So now I call it: > > ok=t_appndf(Logfile,"startet\n") > ok=t_appndf(Logfile,"ended\n") > > Now lets look at the resulting Logfile: > " > ended > startet > " > Oh no - it "appended things" at the beginning of the file ! > Whats my mistake here ? > > PS: Sent a short copy of the mailto:atp at c10.de thanks > > --Theo Gottwald > http://www.theogott.de > mailto:atg at theogott.de > >
5. Re: how to append at the end of file ?
- Posted by Ad Rienks <kwibus at ZONNET.NL> Dec 12, 2000
- 333 views
You are right, Dan. The close() should be inside the 'open succeeds'-part. ----- Oorspronkelijk bericht ----- Van: Dan B Moyer <DANMOYER at PRODIGY.NET> Aan: <EUPHORIA at LISTSERV.MUOHIO.EDU> Verzonden: dinsdag 12 december 2000 13:53 Onderwerp: Re: how to append at the end of file ? > Rolf, > > I was sure that your new code was wrong, when I saw that you closed the > logfile without regard for whether it had passed or failed the open test, > because every time I have used an open file to test for the existence of a > file, if I closed for a file which didn't exist, I got an error. So, I read > the fine manual :) , and discovered that for append or write, a file *will* > be opened if there was none, so there should (probably?) never be any reason > the open would ever fail, and so closing it should never be a problem. But > with open for read or "update" (reading and then writing?), if you can't > open a file because it doesn't exist, then telling it to close would > generate an error. > > But even though it works, if you're going to put a test for open file in, > shouldn't you put the close inside the "open succeeds" portion of the test, > rather than outside both portions of that test, if for no other reason than > to remind yourself that it must be there for open read? > > I notice Ad did it the same as you did, so I'm probably wrong, but I'll ask > anyway. > > Dan Moyer > > > ----- Original Message ----- > From: "Rolf Schroeder" <rolf.schroeder at DESY.DE> > To: <EUPHORIA at LISTSERV.MUOHIO.EDU> > Sent: Tuesday, December 12, 2000 4:23 AM > Subject: Re: how to append at the end of file ? > > > > "Gottwald, IT-IS T500, Fa. Compaq, DA" wrote: > > > > > > If I believe in the Documentation .. its easy: > > > "Output to a file opened for append will start at the end of file. " > > > Let's try this under Win2k: > > > > > > global function t_appndf(sequence fn,object t) > > > atom fhw,r,f > > > fhw = open(fn,"a") > > > if fhw=-1 then r=0 else r=1 > > > puts(fhw,t) > > > end if > > > return r > > > end function > > > > > > So now I call it: > > > > > > ok=t_appndf(Logfile,"startet\n") > > > ok=t_appndf(Logfile,"ended\n") > > > > > > Now lets look at the resulting Logfile: > > > " > > > ended > > > startet > > > " > > > Oh no - it "appended things" at the beginning of the file ! > > > Whats my mistake here ? > > > > > > PS: Sent a short copy of the mailto:atp at c10.de thanks > > > > > > --Theo Gottwald > > > http://www.theogott.de > > > mailto:atg at theogott.de > > > > > > ------------------------------------------------------------------------ > > > > > > Gottwald, IT-IS T500, Fa. Compaq, DA.vcfName: Gottwald, IT-IS T500, > Fa. Compaq, DA.vcf > > > Type: VCard (text/x-vcard) > > > > Hi Gottwald, > > > > I changed a little bit your code...: > > --------------------------------------------------- > > global function t_appndf(sequence fn,object t) > > > > atom fhw, r > > > > fhw = open(fn,"a") > > if fhw=-1 then -- open fails... > > r=0 > > else r=1 > > puts(fhw,t) > > end if > > close(fhw) > > return r > > end function > > --------------------------------------------------- > > -- So now I call it: > > > > integer ok > > > > ok=t_appndf("Logfile","startet\n") > > ok=t_appndf("Logfile","ended\n") > > --------------------------------------------------- > > > > Have a nice day, Rolf
6. Re: how to append at the end of file ?
- Posted by Ad Rienks <kwibus at ZONNET.NL> Dec 12, 2000
- 351 views
Hallo Theo, It is easy. The crucial thing is to close your file again before appending a second item: -- Append2File.ex global function t_appndf(sequence fn, object t) atom fhw,r fhw = open(fn,"a") if fhw = -1 then r = 0 else r = 1 puts(fhw, t) end if close(fhw) -- this is necessary! return r end function -- So now I call it: object ok, Logfile Logfile = "test.dat" ok = t_appndf(Logfile, "started\n") ok = t_appndf(Logfile, "ended\n") -- EOF Append2File.ex Ad Rienks ----- Oorspronkelijk bericht ----- Van: Gottwald, IT-IS T500, Fa. Compaq, DA <T.Gottwald at DEUTSCHEPOST.DE> Aan: <EUPHORIA at LISTSERV.MUOHIO.EDU> Verzonden: dinsdag 12 december 2000 12:14 Onderwerp: how to append at the end of file ? > If I believe in the Documentation .. its easy: > "Output to a file opened for append will start at the end of file. " > Let's try this under Win2k: > > global function t_appndf(sequence fn,object t) > atom fhw,r,f > fhw = open(fn,"a") > if fhw=-1 then r=0 else r=1 > puts(fhw,t) > end if > return r > end function > > So now I call it: > > ok=t_appndf(Logfile,"startet\n") > ok=t_appndf(Logfile,"ended\n") > > Now lets look at the resulting Logfile: > " > ended > startet > " > Oh no - it "appended things" at the beginning of the file ! > Whats my mistake here ? > > PS: Sent a short copy of the mailto:atp at c10.de thanks > > --Theo Gottwald > http://www.theogott.de > mailto:atg at theogott.de > >