怎样快速生成一个指定大小的文件

zhaohao · · 4102 次点击
如果我没记错的话,seek 文件到指定位置并写入一个字节,即可生成指定大小的文件。。。。
#4
更多评论
```golang package main import ( "os" "math" "strings" "fmt" ) func createFile(size float64,filename string){ size = math.Ceil(size) fmt.Println(size) f,err := os.OpenFile(filename,os.O_CREATE|os.O_APPEND|os.O_RDWR,os.ModeAppend|os.ModeSetuid) if err != nil{ panic(err) } defer f.Close() count := math.Ceil(float64(size) / 1000) count_64 := int64(int(count)) var i int64 var length int for i = 0;i<count_64;i++{ if i == (count_64-1){ length = int(int64(size) - (i) *1000) } else { length = 1000 } f.WriteAt([]byte(strings.Repeat("A",length)),i*1000) } } func main(){ createFile(5.1*1024*1024,"data/file3.txt") fmt.Println("OK") } ``` 这样的算吗?
#1
```golang package main import ( "os" "math" "strings" "fmt" ) func createFile(size float64,filename string){ size = math.Ceil(size) fmt.Println(size) f,err := os.OpenFile(filename,os.O_CREATE|os.O_APPEND|os.O_RDWR,os.ModeAppend|os.ModeSetuid) if err != nil{ panic(err) } defer f.Close() count := math.Ceil(float64(size) / 1000) count_64 := int64(int(count)) var i int64 var length int for i = 0;i<count_64;i++{ if i == (count_64-1){ length = int(int64(size) - (i) *1000) } else { length = 1000 } f.WriteAt([]byte(strings.Repeat("A",length)),i*1000) } } func main(){ createFile(5.1*1024*1024,"data/file3.txt") fmt.Println("OK") } ```
#2