Skip to content

Instantly share code, notes, and snippets.

@shantanusingh
Created July 19, 2012 15:36
Show Gist options
  • Save shantanusingh/3144749 to your computer and use it in GitHub Desktop.
Save shantanusingh/3144749 to your computer and use it in GitHub Desktop.
Writes an output file of desired size ( flags check pending)
package main
import(
"io"
"os"
"fmt"
"flag"
)
var size = flag.Int64("size", 1024, "Size of the file to create")
var oName = flag.String("filename", "C:\\output.txt", "Name of the file to create")
func usage(){
fmt.Fprintf(os.Stderr, "usage: %s [size] [filename]\n" , os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
func main() {
flag.Usage = usage
flag.Parse()
fmt.Println("length", len(flag.Args()));
//read from file
fi, err := os.Open("D:\\work\\cleanup.sql")
if err != nil { panic(err) }
defer fi.Close()
fo, err := os.Create(*oName)
if err != nil { panic(err) }
defer fo.Close()
buf := make([]byte, 4096)
n, err := fi.Read(buf)
if err != nil && err != io.EOF { panic(err) }
//if n == 0 { panic() }
for {
if n2, err := fo.Write(buf[:n]); err != nil {
panic(err)
}else if n2 != 0{
info, err2 := os.Stat(*oName)
if(err2 != nil) {
panic(err2)
}
if info.Size() >= *size {
fmt.Printf(" breaking ")
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment