how to write file appendly

7,809 views
Skip to first unread message

xf wang

unread,
Sep 12, 2010, 12:05:19 AM9/12/10
to golan...@googlegroups.com
   ioutil.WriteFile(filename string, data []byte, perm uint32)

when using ioutil.WriteFile(fileName,[]byte(err),0644),

0644 means overwritting,

anyone know how to append file instead of overwitting?


Russ Cox

unread,
Sep 12, 2010, 12:38:09 AM9/12/10
to xf wang, golan...@googlegroups.com
WriteFile always overwrites.
To append you have to open the file yourself.
Copy the source to WriteFile and open it with
os.O_APPEND instead of os.O_TRUNC.

godoc -src io/ioutil WriteFile

Russ

xf wang

unread,
Sep 19, 2010, 5:58:29 AM9/19/10
to r...@golang.org, golan...@googlegroups.com
f_price,_:=os.Open("history.txt",os.O_APPEND|os.O_RDWR,0666)

io.WriteString(f_price,str )

it is still overwritten instead of being appended?

peterGo

unread,
Sep 19, 2010, 9:41:01 AM9/19/10
to golang-nuts
It works.

package main

import ("fmt";"io";"io/ioutil";"os")

const file = "temp.txt"

func write(flag int, text string) {
f, err:=os.Open(file, flag, 0666)
if err != nil { fmt.Println(err); return }
n, err := io.WriteString(f, text)
if err != nil { fmt.Println(n, err); return }
f.Close()
data, err := ioutil.ReadFile(file)
if err != nil { fmt.Println(err); return }
fmt.Println(string(data))
}

func main() {
write(os.O_CREAT|os.O_TRUNC|os.O_RDWR, "new")
for i := 0; i < 2; i++ {
write(os.O_APPEND|os.O_RDWR, "|append")
}
}

Output:
new
new|append
new|append|append

Peter

xf wang

unread,
Sep 19, 2010, 8:13:45 PM9/19/10
to peterGo, golan...@googlegroups.com
 i copy your code and run it,
and
output in temp.txt:
|append

environment: windows xp,
golang version:2010-09-06

Rob 'Commander' Pike

unread,
Sep 19, 2010, 8:38:32 PM9/19/10
to xf wang, peterGo, golan...@googlegroups.com
That sounds like a bug in the Windows version.

-rob

brainman

unread,
Sep 19, 2010, 10:32:47 PM9/19/10
to golang-nuts
Thank you for the test case <g>!

http://code.google.com/p/go/issues/detail?id=1124

brainman

unread,
Sep 19, 2010, 10:33:42 PM9/19/10
to golang-nuts
On Sep 20, 10:13 am, xf wang <wxf6...@gmail.com> wrote:
>  i copy your code and run it,

Looks like a bug:

http://code.google.com/p/go/issues/detail?id=1124

Alex

Skip Tavakkolian

unread,
Sep 20, 2010, 12:59:59 AM9/20/10
to brainman, golang-nuts
i think a problem is in src/pkg/syscall/syscall_windows.go in function
Open (sorry, i can't build Go on windows yet):

func Open(path string, mode int, perm uint32) (fd int, errno int) {
if len(path) == 0 {
return -1, ERROR_FILE_NOT_FOUND
}
var access uint32
switch mode & (O_RDONLY | O_WRONLY | O_RDWR) {
case O_RDONLY:
access = GENERIC_READ
case O_WRONLY:
access = GENERIC_WRITE
case O_RDWR:
access = GENERIC_READ | GENERIC_WRITE
}
// should probably add the following
if mode&O_APPEND {
access |= FILE_APPEND_DATA
// and probably should also do:
// access &= ^GENERIC_WRITE
}

-Skip

brainman

unread,
Sep 20, 2010, 1:07:23 AM9/20/10
to golang-nuts

On Sep 20, 2:59 pm, Skip Tavakkolian <skip.tavakkol...@gmail.com>
wrote:
> i think a problem is in src/pkg/syscall/syscall_windows.go in function
> Open (sorry, i can't build Go on windows yet):
>

Yes, it looks like O_APPEND have not been implemented yet.

> // should probably add the following
>         if mode&O_APPEND {
>                 access |= FILE_APPEND_DATA
>                 // and probably should also do:
>                 // access &= ^GENERIC_WRITE
>         }

I'm not sure if it is possible. But, if nothing else, WriteFile with
special offset of 0xFFFFFFFF (see lpOverlapped) parameter should do
that trick.

Thanks for your suggestion.

Alex
Reply all
Reply to author
Forward
0 new messages