英文:
How do I save an entire string as a txt file in Go?
问题
我正在创建一个简单的Go文字处理程序。从命令行开始,我有两个提示:
$输入标题:
$输入正文:
该程序应该将文档保存为txt文件并打印到命令行。如果用户输入一个单词的标题和一个单词的正文,程序可以正常工作。但是,如果用户输入一个由多个单词组成的标题,会出现以下情况:
$输入标题:这是一个标题
$输入正文:s
$ 标题
-bash: 标题: 找不到命令
以下是我目前的代码:
package main
import (
"fmt"
"io/ioutil"
)
//创建文档的结构体
type Document struct {
Title string
Body []byte
}
//将文档保存为txt文件
func (p *Document) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}
//加载文档
func loadPage(title string) (*Document, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Document{Title: title, Body: body}, nil
}
//输入文档的标题和正文
func main() {
fmt.Print("输入标题:")
var Title string
fmt.Scanln(&Title)
fmt.Print("输入正文:")
var Body []byte
fmt.Scanln(&Body)
//保存文档并在命令行上显示
p1 := &Document{Title: Title, Body: []byte(Body)}
p1.save()
p2, _ := loadPage(Title)
fmt.Println(string(p2.Body))
}
英文:
I'm creating a simple word processing program in Go. From the command line, I have two prompts:
$Enter Title:
$Enter Body:
The program is supposed to save the document as a txt file and print it to the command line. The program works if the user user types in a one-word Title and a one-word Body. But if the user types in a several-word title, this will happen:
$Enter Title: Here is a title
$Enter Body: s
$ title
-bash: title: command not found
Here is the code I have so far:
package main
import (
"fmt"
"io/ioutil"
)
//Create struct for a document
type Document struct {
Title string
Body []byte
}
//Save document as txt file
func (p *Document) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}
//Load document
func loadPage(title string) (*Document, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Document{Title: title, Body: body}, nil
}
//Input document title and body.
func main() {
fmt.Print("Enter Title: ")
var Title string
fmt.Scanln(&Title)
fmt.Print("Enter Body: ")
var Body []byte
fmt.Scanln(&Body)
//Save document and display on command line
p1 := &Document{Title: Title, Body: []byte(Body)}
p1.save()
p2, _ := loadPage(Title)
fmt.Println(string(p2.Body))
}
答案1
得分: 0
使用bufio.ReadString
代替fmt.Scanln
怎么样?
不完全了解Scanln的工作原理,但我相当确定问题出在对该函数的误用上。
使用bufio的示例代码:
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
// Document代表文档的数据。
type Document struct {
Title string
Body []byte
}
// save将文档以txt文件的形式保存到磁盘上。
func (p *Document) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}
// loadPage从磁盘加载文档。
func loadPage(title string) (*Document, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Document{Title: title, Body: body}, nil
}
// 输入文档的标题和内容。
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("输入标题:")
title, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
title = strings.TrimSpace(title)
fmt.Print("输入内容:")
body, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
body = strings.TrimSpace(body)
// 保存文档并在命令行上显示
p1 := &Document{Title: title, Body: []byte(body)}
if err := p1.save(); err != nil {
log.Fatal(err)
}
p2, err := loadPage(title)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(p2.Body))
}
英文:
What about using bufio.ReadString
instead of fmt.Scanln
?
Not 100% how Scanln works, but I am pretty sure the issue comes from a misuse of that function.
Example with bufio:
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
// Document represent the document's data.
type Document struct {
Title string
Body []byte
}
// Save dumps document as txt file on disc.
func (p *Document) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}
// loadPage loads a document from disc.
func loadPage(title string) (*Document, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Document{Title: title, Body: body}, nil
}
// Input document title and body.
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Title: ")
title, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
title = strings.TrimSpace(title)
fmt.Print("Enter Body: ")
body, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
body = strings.TrimSpace(body)
//Save document and display on command line
p1 := &Document{Title: title, Body: []byte(body)}
if err := p1.save(); err != nil {
log.Fatal(err)
}
p2, err := loadPage(title)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(p2.Body))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论