dsen53898 2016-04-01 12:38
浏览 32
已采纳

Golang扫描不扫描下一行

This scanner dosent scan for the next line. I will explain it in more detail when you see results...

package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "strings"
)

func main() {
    var inputFileName string
    var write string

    fmt.Scanln(&inputFileName)

    //func Join(a []string, sep string) string
    s := []string{inputFileName, ".txt"}
    inputFileName = strings.Join(s, "")

    creator, err := os.Create(inputFileName)
    check(err)

    /*
     *Writing
     */

    fmt.Printf("The file name with %s what do you want to write?", inputFileName)
    fmt.Scanln(&write)
    if len(write) <= 0 {
        panic("Cant be empty")
    }

    byteStringWrite := []byte(write)
    //func (f *File) Write(b []byte) (n int, err error)
    fmt.Println("BYTE : ", byteStringWrite)
    fmt.Println("NONBYTE : ", write)
    _, errWriter := creator.Write(byteStringWrite)
    check(errWriter)

    /**
     *Reading File
     */

    read, errRead := ioutil.ReadFile(inputFileName)
    check(errRead)
    readString := string(read)
    fmt.Println("*******************FILE*********************")
    fmt.Println(readString)
}

func check(e error) {
    if e != nil {
        panic(e)
    }
}

Results:

Sample.txt //My User Input
The file name with Sample.txt what do you want to write?Hello World
BYTE :  [72 101 108 108 111]
NONBYTE :  Hello
*******************FILE*********************
Hello

So Here you can see it dosent look for the space. Meaning after the space it automatically quits. Can someone help me figure out this problem? Thankyou.

EDIT

Using bufio.ReadString();

package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "strings"
    "bufio"
)

func main() {
    var inputFileName string
    var write string

    bio := bufio.NewReader(os.Stdin)
    inputFileName, err := bio.ReadString('
')
    fmt.Println(inputFileName)

    //func Join(a []string, sep string) string
    s := []string{inputFileName, ".txt"}
    inputFileName = strings.Join(s, "")

    creator, err := os.Create(inputFileName)
    check(err)

    /*
     *Writing
     */

    fmt.Printf("The file name with %s what do you want to write?", inputFileName)
    fmt.Scanln(&write)
    if len(write) <= 0 {
        panic("Cant be empty")
    }

    byteStringWrite := []byte(write)
    //func (f *File) Write(b []byte) (n int, err error)
    fmt.Println("BYTE : ", byteStringWrite)
    fmt.Println("NONBYTE : ", write)
    _, errWriter := creator.Write(byteStringWrite)
    check(errWriter)

    /**
     *Reading File
     */

    read, errRead := ioutil.ReadFile(inputFileName)
    check(errRead)
    readString := string(read)
    fmt.Println("*******************FILE*********************")
    fmt.Println(readString)
}

func check(e error) {
    if e != nil {
        panic(e)
    }
}

Results:

amanuel2:~/workspace/pkg_os/07_Practice $ go run main.go 
Sample
The file name with Sample
.txt what do you want to write?Something Else
BYTE :  [83 111 109 101 116 104 105 110 103]
NONBYTE :  Something
*******************FILE*********************
Something

Gives me correct .txt .. But same issue as above, it dosent take spaces

  • 写回答

1条回答 默认 最新

  • du22399 2016-04-01 12:53
    关注

    This is exactly what fmt.Scanln is supposed to do:

    Scan scans text read from standard input, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.

    If you want to read a line of text use bufio.Reader:

    bio := bufio.NewReader(os.Stdin)
    
    // in case you want a string which doesn't contain the newline
    line, hasMoreInLine, err := bio.ReadLine()
    s := string(line)    
    fmt.Println(s)
    
    // in case you need a string which contains the newline
    s, err := bio.ReadString('
    ')
    fmt.Println(s)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题