Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. // reads the file txt.txt
  2. bs, err := ioutil.ReadFile("text.txt")
  3. if err != nil {
  4. panic(err)
  5. }
  6.  
  7. // Prints out content
  8. textInFile := string(bs)
  9. fmt.Println(textInFile)
  10.  
  11. // Standard input from keyboard
  12. var userInput string
  13. fmt.Scanln(&userInput)
  14.  
  15. //Now I want to write input back to file text.txt
  16. //func WriteFile(filename string, data []byte, perm os.FileMode) error
  17.  
  18. inputData := make([]byte, len(userInput))
  19.  
  20. err := ioutil.WriteFile("text.txt", inputData, )
  21.  
  22. // reads the file txt.txt
  23. bs, err := ioutil.ReadFile("text.txt")
  24. if err != nil { //may want logic to create the file if it doesn't exist
  25. panic(err)
  26. }
  27.  
  28. var userInput []string
  29.  
  30. var err error = nil
  31. var n int
  32. //read in multiple lines from user input
  33. //until user enters the EOF char
  34. for ln := ""; err == nil; n, err = fmt.Scanln(ln) {
  35. if n > 0 { //we actually read something into the string
  36. userInput = append(userInput, ln)
  37. } //if we didn't read anything, err is probably set
  38. }
  39.  
  40. //open the file to append to it
  41. //0666 corresponds to unix perms rw-rw-rw-,
  42. //which means anyone can read or write it
  43. out, err := os.OpenFile("text.txt", os.O_APPEND, 0666)
  44. defer out.Close() //we'll close this file as we leave scope, no matter what
  45.  
  46. if err != nil { //assuming the file didn't somehow break
  47. //write each of the user input lines followed by a newline
  48. for _, outLn := range userInput {
  49. io.WriteString(out, outLn+"n")
  50. }
  51. }
  52.  
  53. // Read old text
  54. current, err := ioutil.ReadFile("text.txt")
  55.  
  56. // Standard input from keyboard
  57. var userInput string
  58. fmt.Scanln(&userInput)
  59.  
  60. // Append the new input to the old using builtin `append`
  61. newContent := append(current, []byte(userInput)...)
  62.  
  63. // Now write the input back to file text.txt
  64. err = ioutil.WriteFile("text.txt", newContent, 0666)
  65.  
  66. // Open the file for read and write (O_RDRW), append to it if it has
  67. // content, create it if it does not exit, use 0666 for permissions
  68. // on creation.
  69. file, err := os.OpenFile("text.txt", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
  70.  
  71. // Close the file when the surrounding function exists
  72. defer file.Close()
  73.  
  74. // Read old content
  75. current, err := ioutil.ReadAll(file)
  76.  
  77. // Do something with that old content, for example, print it
  78. fmt.Println(string(current))
  79.  
  80. // Standard input from keyboard
  81. var userInput string
  82. fmt.Scanln(&userInput)
  83.  
  84. // Now write the input back to file text.txt
  85. _, err = file.WriteString(userInput)
  86.  
  87. package main
  88.  
  89. import (
  90. "fmt"
  91. "io/ioutil"
  92. "os"
  93. )
  94.  
  95. func main() {
  96. fname := "text.txt"
  97.  
  98. // print text file
  99. textin, err := ioutil.ReadFile(fname)
  100. if err == nil {
  101. fmt.Println(string(textin))
  102. }
  103.  
  104. // append text to file
  105. f, err := os.OpenFile(fname, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
  106. if err != nil {
  107. panic(err)
  108. }
  109. var textout string
  110. fmt.Scanln(&textout)
  111. _, err = f.Write([]byte(textout))
  112. if err != nil {
  113. panic(err)
  114. }
  115. f.Close()
  116.  
  117. // print text file
  118. textin, err = ioutil.ReadFile(fname)
  119. if err != nil {
  120. panic(err)
  121. }
  122. fmt.Println(string(textin))
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement