写完后不能从文件中读取字节

时间:2017-01-14 07:35:35

标签: python go io byte

所以,我试图在golang中创建一个简单的AOT虚拟机,它读取一个字节码文件作为它的输入。我正在尝试非常基本上,将字节写入文件然后使用ioutil读取它们,但是我遇到了空的解除引用错误。

这是我用于写入文件的python代码:

btest = open("test.thief", "w")
bytes_to_write = bytearray([1, 44, 56, 55, 55, 0])

btest.write(bytes_to_write)

btest.close()

这是我用来读取字节

的go文件中的代码
package main

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

func main() {
  //gets command line args
    userArgs := os.Args[1:]

    bytes, err := ioutil.ReadFile(userArgs[0]) // just pass the file name
    if err != nil {
        fmt.Print(err)
    }

     fmt.Println(bytes)
     machine := NewEnv()
     ReadBytes(machine, bytes)

}

这是我得到的错误:

Joshuas-MacBook-Pro-3:thief Josh$ ./bin/Thief test.thief
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x2af3]

goroutine 1 [running]:
main.ReadBytes(0xc82000e390, 0xc82000c480, 0x6, 0x206)
    /Users/Josh/thief/src/Thief/read.go:26 +0x353
main.main()
    /Users/Josh/thief/src/Thief/Main.go:18 +0x1f2

我也尝试使用python代码以二进制模式打开文件,但它会产生相同的错误。基本上,我的目标是我希望能够提取一个字节数组,但也有办法写主题。

我写的字节不正确吗?

这是hexdump:

012c 3837 3700

编辑:

这是我的其他文件

opfuncs.go

package main


//file for machine functions and operations

//prints string format of some bytes
func print(env Env, data []byte) {
    fmt.Println(string(data))
}


var OPERS map[byte]func(Env, []byte)

//0 is reserved as the null terminator
func init(){
    OPERS = map[byte]func(Env, []byte){
        1:print,
    }
}

env.go

package main


//file for the env struct



type Env struct {
    items map[string]interface{}
}

func NewEnv() Env {
    return Env{make(map[string]interface{})}
}

//general getter
func (self *Env) get(key string) interface{} {
    val, has := self.items[key]
    if has {
        return val
    } else {
        return nil
    }
}

//general setter
func (self *Env) set(key string, value interface{}) {
    self.items[key] = value
}

func (self *Env) contains(key string) bool {
    _, has := self.items[key]
    return has
}

func (self *Env) declare(key string) {
    self.items[key] = Null{}
}

func (self *Env) is_null(key string) bool {
    _, ok := self.items[key].(Null)
    return ok
}

//deletes an item from storage
func (self *Env) del(key string) {
    delete(self.items, key)
}

read.go

package main

//file that contains the reading function for the VM

func ReadBytes(env Env, in []byte) {
    bytes := make([]byte, 1)
    fn := byte(0)
    mode := 0
    for i:=0;i<len(in);i++ {
        switch mode {
        case 0:
            fn = byte(i)
            mode = 1
        case 1:
            if i != len(in)-1 {
                if in[i+1] == 0 {
                    bytes = append(bytes, in[i])
                    mode = 2
                } else {
                    bytes = append(bytes, in[i])
                }
            } else {
                bytes = append(bytes, in[i])
            }
        case 2:
            OPERS[fn](env, bytes)
            mode = 0
            fn = byte(0)
            bytes = make([]byte, 1)
        }
    }
}

2 个答案:

答案 0 :(得分:2)

如果使用Python 3,则必须以二进制模式打开文件:

yarn test

答案 1 :(得分:0)

看起来您没有在python代码中写入文件?

我得到了

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not bytearray

在您的第三行之后,这将解释为什么golang无法找到文件内容。

此外,您没有使用bytes,因此您的gocode将无法编译。这样做。

package main

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

func main() {
  //gets command line args
    userArgs := os.Args[1:]

    _, err := ioutil.ReadFile(userArgs[0]) // just pass the file name
    if err != nil {
        fmt.Print(err)
    }

}