Unwanted trailing byte when writing to binary file

210 views
Skip to first unread message

Ondekoza

unread,
Oct 15, 2012, 10:33:57 AM10/15/12
to golan...@googlegroups.com
When I write an array of byte to a binary file I get an extra byte '6F' at the end. Why is that and how do I get rid of that?

package main
import "os"
import "fmt"
func main() {
    ff, _ := os.OpenFile("output", os.O_WRONLY|os.O_CREATE, 0)
    defer ff.Close()
    a := []byte{0x1, 0x2, 0x3, 0x4}
    x, y := ff.Write(a)
    fmt.Printf("%v %v", x, y)
}

This prints
4 <nil>
as expected. But the file has 5 bytes and 'od -tx1 output' tells me this:
0000000 01 02 03 04 6f

I though it might have something to do with windows-type text mode adding a newline, but 6F is a lowercase ASCII 'o'...

Stefan


Rob Lapensee

unread,
Oct 15, 2012, 10:44:20 AM10/15/12
to golan...@googlegroups.com
no extra characters on my test,
using go version 1.0.2 on windows XP (virtual XP-mode) in cygwin

$ c:/go/bin/go version
go version go1.0.2

$ c:/go/bin/go run x.go
4 <nil>

$ od -c output
0000000 001 002 003 004
0000004

$ uname -a
CYGWIN_NT-5.1 maestro 1.7.16(0.262/5/3) 2012-07-20 22:55 i686 Cygwin

what version OS and go are you using?

Regards,

Rob

Dumitru Ungureanu

unread,
Oct 15, 2012, 10:49:02 AM10/15/12
to golan...@googlegroups.com
Win7 64bit
Linux 32bit

4 bytes, no extra

Jan Mercl

unread,
Oct 15, 2012, 10:56:28 AM10/15/12
to Ondekoza, golan...@googlegroups.com
You have to delete the file "output" or truncate it to some desired
size before running your program and expecting the file size to be
equal what's written into it by the last program.

-j

Dumitru Ungureanu

unread,
Oct 15, 2012, 11:05:08 AM10/15/12
to golan...@googlegroups.com, Ondekoza
Good catch Jan.

If the "output" had the "hello" string in it (a wild guess), then it makes sense it would hold "01 02 03 04 o" after overwriting the first 4 bytes..

roger peppe

unread,
Oct 15, 2012, 11:07:51 AM10/15/12
to Ondekoza, golan...@googlegroups.com
The others have it right. Use os.Create or add the O_TRUNC flag.
> --
>
>

Ondekoza

unread,
Oct 15, 2012, 11:09:51 AM10/15/12
to golan...@googlegroups.com, Ondekoza
Well done, Sherlock. :-)

Ondekoza

unread,
Oct 15, 2012, 11:15:55 AM10/15/12
to golan...@googlegroups.com, Ondekoza
So, what is the proper idiom to create a file from scratch and overwrite if it exists?

Do I really have to go to the steps

Check existence of file.
Delete file
Check if deletion worked.
open file with os.Create?

Then the obvious follow-up question is: Is Create equivalent to Open with O_TRUNC and if so, why doesn't Create have second signature like so:
func Create(name string, flag int, perm FileMode) (file *File, err error)


roger peppe

unread,
Oct 15, 2012, 11:18:25 AM10/15/12
to Ondekoza, golan...@googlegroups.com
It's really not too hard to click the "Create" link in the documentation,
which takes you to this code:

http://golang.org/src/pkg/os/file.go?s=7058:7106#L228

The reason Create was defined with no arguments is that the
implied permissions and mode are by far the most common way
to create a file. If you need a different mode or different flags,
you know where to find Open.

Ondekoza

unread,
Oct 15, 2012, 11:23:51 AM10/15/12
to golan...@googlegroups.com, Ondekoza
Agreed.

OTOH the Create function seems kind of superfluous.
Having "Open" to handle file opening (and all the associated options) seems to adhere to the good ol' principle of least surprise.

roger peppe

unread,
Oct 15, 2012, 11:34:10 AM10/15/12
to Ondekoza, golan...@googlegroups.com
On 15 October 2012 16:23, Ondekoza <onde...@gmail.com> wrote:
> Agreed.
>
> OTOH the Create function seems kind of superfluous.
> Having "Open" to handle file opening (and all the associated options) seems
> to adhere to the good ol' principle of least surprise.

It's convenient. Creating a file is not an uncommon operation, and
it's awkward to remember all those flags.

I was wrong above, BTW - Open just opens a file for reading;
I meant to say OpenFile.

Ondekoza

unread,
Oct 15, 2012, 11:51:43 AM10/15/12
to golan...@googlegroups.com, Ondekoza
Using Create it works as expected. Thank you for all the contributions.

Reply all
Reply to author
Forward
0 new messages