如何使用Go语言读取文件的前四个字节?

huangapple go评论89阅读模式
英文:

How to read the first four bytes of a file, using Go?

问题

我正在学习Go,并尝试读取文件的前四个字节。我想要检查文件是否包含我正在寻找的特定文件头。然而,我的代码没有显示我期望的字节。有人知道为什么以下代码可能不起作用吗?它确实读取了一些字节,但它们不是我认识或期望看到的字节。它们不是随机的,因为每次运行时它们都是相同的,所以它可能是指向其他东西的指针或其他东西。

另外,我意识到我忽略了错误,但那是因为在这个问题没有解决之前,我进入了黑客模式,并尽量删除了多余的代码,试图找到问题所在。

package main

import (
    "os"
    "io"
    "fmt"
)

type RoflFile struct {
    identifier []byte
}

func main() {
    arguments := os.Args[1:]

    if len(arguments) != 1 {
        fmt.Println("Usage: <path-to-rofl>")
        return
    }

    inputfile := arguments[0]

    if _, err := os.Stat(inputfile); os.IsNotExist(err) {
        fmt.Printf("Error: the input file could not be found: %s", inputfile)
        return
    }

    rofl := new(RoflFile)
    rofl.identifier = make([]byte, 4)

    // 打开输入文件以便我们可以提取数据
    f, _ := os.Open(inputfile)

    // 读取文件标识符
    io.ReadAtLeast(f, rofl.identifier, 4)

    f.Close()

    fmt.Printf("Got: %+v", rofl)
}
英文:

I'm learning Go, and am trying to read the first four bytes of a file. I'm wanting to check if the file contains a specific file header that I'm looking for. My code does not display the bytes that I'm expecting, though. Does anybody know why the following code might not work? It does read in some bytes, but they're not bytes I recognized or expected to see. They're not random or anything, because they're the same every time I run it, so it's probably a pointer to something else or something.

Also, I realize I'm ignoring errors but that's because I went into hack-mode while this wasn't working and removed as much of the cruft as I could, trying to get down to the issue.

package main

import (
	
	&quot;os&quot;
	&quot;io&quot;
	&quot;fmt&quot;

)

type RoflFile struct {

	identifier []byte

}

func main() {

	arguments := os.Args[1:]

	if len(arguments) != 1 {

		fmt.Println(&quot;Usage: &lt;path-to-rofl&gt;&quot;)
		return

	}

	inputfile := arguments[0]

	if _, err := os.Stat(inputfile); os.IsNotExist(err) {

	    fmt.Printf(&quot;Error: the input file could not be found: %s&quot;, inputfile)
	    return

	}

	rofl := new(RoflFile)
	rofl.identifier = make([]byte, 4)

	// open the input file so that we can pull out data
	f, _ := os.Open(inputfile)

	// read in the file identifier
	io.ReadAtLeast(f, rofl.identifier, 4)

	f.Close()

	fmt.Printf(&quot;Got: %+v&quot;, rofl)

}

答案1

得分: 6

当我运行您的代码对以“9876”开头的输入文件进行测试时,我得到:

<pre>
得到:&{identifier:[57 56 55 54]}
</pre>

当对以“1234”开头的输入文件运行时,我得到:

<pre>
得到:&{identifier:[49 50 51 52]}
</pre>

对我来说,程序按预期工作。要么在您的系统上出了问题,要么您没有意识到您得到的是文件中前四个字节的十进制值。您是期望得到十六进制吗?还是您期望根据某种编码(例如ASCII或UTF-8)解释字节(例如看到“9 8 7 6”而不是“57 56 55 54”)?

以后参考(或者如果这个回答没有解决您的问题),在这些情况下,包括您的输入文件、您在系统上得到的输出和您期望的输出是有帮助的。“它们不是我认识或期望看到的字节”会有很多可能性。

英文:

When I run your code against an input file beginning with "9876", I get:

<pre>
Got: &{identifier:[57 56 55 54]}
</pre>

When run against an input file beginning with "1234", I get:

<pre>
Got: &{identifier:[49 50 51 52]}
</pre>

For me, the program works as expected. Either something is going wrong on your system, or you don't realize that you're getting the decimal value of the first four bytes in the file. Were you expecting hex? Or were you expecting to see the bytes interpreted according to some encoding (e.g., ASCII or UTF-8, seeing "9 8 7 6" instead of "57 56 55 54")?

For future reference (or if this didn't answer your question), it's helpful in these situations to include your input file, the output you get on your system, and the output you expected. "They're not bytes I recognized or expected to see" leaves a lot of possibilities on the table.

huangapple
  • 本文由 发表于 2013年5月24日 07:18:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/16725439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定