Commits

Dustin Chapman committed b992240

simple file copy

  • Participants
  • Parent commits 22a222b

Comments (0)

Files changed (1)

 package main
 
 import (
+	"bufio"
 	"flag"
 	"fmt"
+	"io"
+	"os"
 )
 
 var (
 func main() {
 	flag.Parse()
 	fmt.Println("infile:", *infile)
+	fmt.Println("outfile:", *outfile)
+
+	fi, err := os.Open(*infile)
+	if err != nil {
+		panic(err)
+	}
+	defer fi.Close()
+	r := bufio.NewReader(fi)
+
+	fo, err := os.Create(*outfile)
+	if err != nil {
+		panic(err)
+	}
+	defer fo.Close()
+	w := bufio.NewWriter(fo)
+
+	buf := make([]byte, 1024)
+
+	for {
+		nr, err := r.Read(buf)
+		if err != nil && err != io.EOF {
+			panic(err)
+		}
+		if nr == 0 {
+			break
+		}
+
+		nw, err := w.Write(buf[:nr])
+		if err != nil {
+			panic(err)
+		} else if nw != nr {
+			panic("error writing outfile")
+		}
+
+		if err = w.Flush(); err != nil {
+			panic(err)
+		}
+	}
 }