Guest User

Untitled

a guest
Jan 24th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "os"
  5. "bufio"
  6. )
  7.  
  8. const SEP = '\n'
  9.  
  10. func lineChangeFunction(line string) string {
  11. return line
  12. }
  13.  
  14. func changeFile(inputFilename string, outputFilename string, lineChangeFunction func(string) string) bool {
  15. inputFile, err := os.Open(inputFilename, os.O_RDONLY, 0)
  16. if err != nil {
  17. return false
  18. }
  19. defer inputFile.Close()
  20. outputFile, err := os.Open(outputFilename, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0666)
  21. if err != nil {
  22. return false
  23. }
  24. defer outputFile.Close()
  25. in := bufio.NewReader(inputFile)
  26. for {
  27. line, err := in.ReadSlice(SEP);
  28. if err != nil {
  29. break
  30. };
  31. outputFile.WriteString(lineChangeFunction(string(line)))
  32. }
  33. return true
  34. }
  35.  
  36. func main() {
  37. changeFile("input.txt", "output.txt", lineChangeFunction)
  38. }
Add Comment
Please, Sign In to add comment