How do I save an entire string as a txt file in Go?

huangapple go评论109阅读模式
英文:

How do I save an entire string as a txt file in Go?

问题

我正在创建一个简单的Go文字处理程序。从命令行开始,我有两个提示:

$输入标题:

$输入正文:

该程序应该将文档保存为txt文件并打印到命令行。如果用户输入一个单词的标题和一个单词的正文,程序可以正常工作。但是,如果用户输入一个由多个单词组成的标题,会出现以下情况:

  1. $输入标题:这是一个标题
  2. $输入正文:s
  3. $ 标题
  4. -bash: 标题: 找不到命令

以下是我目前的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. )
  6. //创建文档的结构体
  7. type Document struct {
  8. Title string
  9. Body []byte
  10. }
  11. //将文档保存为txt文件
  12. func (p *Document) save() error {
  13. filename := p.Title + ".txt"
  14. return ioutil.WriteFile(filename, p.Body, 0600)
  15. }
  16. //加载文档
  17. func loadPage(title string) (*Document, error) {
  18. filename := title + ".txt"
  19. body, err := ioutil.ReadFile(filename)
  20. if err != nil {
  21. return nil, err
  22. }
  23. return &Document{Title: title, Body: body}, nil
  24. }
  25. //输入文档的标题和正文
  26. func main() {
  27. fmt.Print("输入标题:")
  28. var Title string
  29. fmt.Scanln(&Title)
  30. fmt.Print("输入正文:")
  31. var Body []byte
  32. fmt.Scanln(&Body)
  33. //保存文档并在命令行上显示
  34. p1 := &Document{Title: Title, Body: []byte(Body)}
  35. p1.save()
  36. p2, _ := loadPage(Title)
  37. fmt.Println(string(p2.Body))
  38. }
英文:

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:

  1. $Enter Title: Here is a title
  2. $Enter Body: s
  3. $ title
  4. -bash: title: command not found

Here is the code I have so far:

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. )
  6. //Create struct for a document
  7. type Document struct {
  8. Title string
  9. Body []byte
  10. }
  11. //Save document as txt file
  12. func (p *Document) save() error {
  13. filename := p.Title + ".txt"
  14. return ioutil.WriteFile(filename, p.Body, 0600)
  15. }
  16. //Load document
  17. func loadPage(title string) (*Document, error) {
  18. filename := title + ".txt"
  19. body, err := ioutil.ReadFile(filename)
  20. if err != nil {
  21. return nil, err
  22. }
  23. return &Document{Title: title, Body: body}, nil
  24. }
  25. //Input document title and body.
  26. func main() {
  27. fmt.Print("Enter Title: ")
  28. var Title string
  29. fmt.Scanln(&Title)
  30. fmt.Print("Enter Body: ")
  31. var Body []byte
  32. fmt.Scanln(&Body)
  33. //Save document and display on command line
  34. p1 := &Document{Title: Title, Body: []byte(Body)}
  35. p1.save()
  36. p2, _ := loadPage(Title)
  37. fmt.Println(string(p2.Body))
  38. }

答案1

得分: 0

使用bufio.ReadString代替fmt.Scanln怎么样?

不完全了解Scanln的工作原理,但我相当确定问题出在对该函数的误用上。
使用bufio的示例代码:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "strings"
  9. )
  10. // Document代表文档的数据。
  11. type Document struct {
  12. Title string
  13. Body []byte
  14. }
  15. // save将文档以txt文件的形式保存到磁盘上。
  16. func (p *Document) save() error {
  17. filename := p.Title + ".txt"
  18. return ioutil.WriteFile(filename, p.Body, 0600)
  19. }
  20. // loadPage从磁盘加载文档。
  21. func loadPage(title string) (*Document, error) {
  22. filename := title + ".txt"
  23. body, err := ioutil.ReadFile(filename)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return &Document{Title: title, Body: body}, nil
  28. }
  29. // 输入文档的标题和内容。
  30. func main() {
  31. reader := bufio.NewReader(os.Stdin)
  32. fmt.Print("输入标题:")
  33. title, err := reader.ReadString('\n')
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. title = strings.TrimSpace(title)
  38. fmt.Print("输入内容:")
  39. body, err := reader.ReadString('\n')
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. body = strings.TrimSpace(body)
  44. // 保存文档并在命令行上显示
  45. p1 := &Document{Title: title, Body: []byte(body)}
  46. if err := p1.save(); err != nil {
  47. log.Fatal(err)
  48. }
  49. p2, err := loadPage(title)
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. fmt.Println(string(p2.Body))
  54. }
英文:

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:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "strings"
  9. )
  10. // Document represent the document's data.
  11. type Document struct {
  12. Title string
  13. Body []byte
  14. }
  15. // Save dumps document as txt file on disc.
  16. func (p *Document) save() error {
  17. filename := p.Title + ".txt"
  18. return ioutil.WriteFile(filename, p.Body, 0600)
  19. }
  20. // loadPage loads a document from disc.
  21. func loadPage(title string) (*Document, error) {
  22. filename := title + ".txt"
  23. body, err := ioutil.ReadFile(filename)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return &Document{Title: title, Body: body}, nil
  28. }
  29. // Input document title and body.
  30. func main() {
  31. reader := bufio.NewReader(os.Stdin)
  32. fmt.Print("Enter Title: ")
  33. title, err := reader.ReadString('\n')
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. title = strings.TrimSpace(title)
  38. fmt.Print("Enter Body: ")
  39. body, err := reader.ReadString('\n')
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. body = strings.TrimSpace(body)
  44. //Save document and display on command line
  45. p1 := &Document{Title: title, Body: []byte(body)}
  46. if err := p1.save(); err != nil {
  47. log.Fatal(err)
  48. }
  49. p2, err := loadPage(title)
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. fmt.Println(string(p2.Body))
  54. }

huangapple
  • 本文由 发表于 2015年6月20日 04:24:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/30946686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定