Go语言的Scan函数无法扫描下一行。

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

Go Lang Scan doesent scan for next line

问题

这个扫描器不会扫描下一行。当你看到结果时,我会详细解释一下...

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)
	}
}

结果:

Sample.txt //我的用户输入
The file name with Sample.txt what do you want to write?Hello World
BYTE :  [72 101 108 108 111]
NONBYTE :  Hello
*******************FILE*********************
Hello

-----

所以你可以看到它不会查找空格。也就是说,在空格之后它会自动退出。有人可以帮我解决这个问题吗?谢谢。

编辑
----

使用[bufio.ReadString()][1]:

```go
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('\n')
    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)
	}
}

结果:

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

给我正确的.txt文件名...但是和上面一样的问题,它不接受空格。
[1]: https://golang.org/pkg/bufio/#Reader.ReadString
英文:

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

package main

import (
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;os&quot;
	&quot;strings&quot;
)

func main() {
	var inputFileName string
	var write string

	fmt.Scanln(&amp;inputFileName)

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

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

	/*
	 *Writing
	 */

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

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

	/**
	 *Reading File
	 */

	read, errRead := ioutil.ReadFile(inputFileName)
	check(errRead)
	readString := string(read)
	fmt.Println(&quot;*******************FILE*********************&quot;)
	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()][1];

package main

import (
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;os&quot;
	&quot;strings&quot;
	&quot;bufio&quot;
)

func main() {
	var inputFileName string
	var write string
	
    bio := bufio.NewReader(os.Stdin)
    inputFileName, err := bio.ReadString(&#39;\n&#39;)
    fmt.Println(inputFileName)

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

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

	/*
	 *Writing
	 */

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

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

	/**
	 *Reading File
	 */

	read, errRead := ioutil.ReadFile(inputFileName)
	check(errRead)
	readString := string(read)
	fmt.Println(&quot;*******************FILE*********************&quot;)
	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]: https://golang.org/pkg/bufio/#Reader.ReadString

答案1

得分: 5

这正是fmt.Scanln应该做的事情:

Scan从标准输入中扫描文本,将连续的以空格分隔的值存储到连续的参数中。换行符被视为空格。它返回成功扫描的项目数。如果成功扫描的项目数少于参数的数量,err将报告原因。

如果你想读取一行文本,请使用bufio.Reader

bio := bufio.NewReader(os.Stdin)

// 如果你想要一个不包含换行符的字符串
line, hasMoreInLine, err := bio.ReadLine()
s := string(line)
fmt.Println(s)

// 如果你需要一个包含换行符的字符串
s, err := bio.ReadString('\n')
fmt.Println(s)
英文:

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&#39;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(&#39;\n&#39;)
fmt.Println(s)

huangapple
  • 本文由 发表于 2016年4月1日 20:38:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/36356573.html
匿名

发表评论

匿名网友

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

确定