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 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解