-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework9.go
55 lines (44 loc) · 981 Bytes
/
homework9.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// import statements
package main
import (
"bufio"
"encoding/hex"
"fmt"
"io"
"log"
"os"
)
// this function opens and prints a binary file
func readBinaryFile(filename string) {
// open the file
file, err := os.Open(filename)
// catch errors
if err != nil {
log.Fatal(err)
}
// specify to close the file later when the function is done
defer file.Close()
// create a new reader for the file
reader := bufio.NewReader(file)
// create slice of bytes
buf := make([]byte, 256)
// loop to read each line of the binary file, and also to catch errors
for {
_, err := reader.Read(buf)
if err != nil {
if err != io.EOF {
fmt.Println(err)
}
break
}
// print each line of the file one at a time
fmt.Printf("%s", hex.Dump(buf))
}
}
// main function to call the function to read and print the file
func main() {
// getting file name from input
filename := os.Args[1]
// calling the function to read the file
readBinaryFile(filename)
}