package main
import (
"fmt"
"os"
)
func appendfile(filename string,content string){
//1、打开文件
f,err:=os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0660)
if err!=nil{
fmt.Printf("can not open the file")
return
}
//2、写入文件
defer f.Close()
f.WriteString(content)
}
// func appendfile(filename string,content string){
// //1、打开文件
// f,err:=os.OpenFile(filename,os.O_WRONLY,0664)
// if err!=nil{
// fmt.Println(err)
// }else{
// //1、找到文件末尾位置
// n,_:=f.Seek(0,os.SEEK_END)
// //2、写入文件的位置
// _,err:=f.WriteAt([]byte(content),n)
// if err!=nil{
// fmt.Println(err)
// }
// }
// defer f.Close()
// }
func main(){
var source string
fmt.Scanf("%s",&source)
appendfile("lines",source)
fmt.Println("write successful")
}
golang写入文件
最新推荐文章于 2025-04-06 18:13:26 发布