Example of Fscan, Fscanf, and Fscanln from FMT Package
Fscan scans text read from r, storing successive space-separated values into successive arguments.
Fscanf scans text read from r, storing successive space-separated values into successive arguments as determined by the format.
Fscanln is similar to Fscan, but stops scanning at a newline and after the final item there must be a newline or EOF.
To execute this program need to create a text file with name testfile.txt and content given in below program comment section.
Example
/* testfile.txt content:
255 5.5 true Australia
200 9.3 false Germany
Paris 5 true 5.5
*/
package main
import (
"fmt"
"os"
)
var (
L int32
M float32
N bool
O string
)
func main() {
file, err := os.Open("testfile.txt")
if err != nil {
panic(err)
}
defer file.Close()
fmt.Fscan(file, &L, &M, &N, &O)
fmt.Printf("\nL M N O: %v %v %v %v", L,M,N,O)
fmt.Fscanln(file, &L, &M, &N, &O)
fmt.Printf("\nL M N O: %v %v %v %v", L,M,N,O)
fmt.Fscanf(file, "%s %d %t %f",&O, &L, &N, &M)
fmt.Printf("\nO L N M: %v %v %v %v", O,L,N,M)
}
Output
L M N O: 255 5.5 true Australia
L M N O: 200 9.3 false Germany
O L N M: Paris 5 true 5.5
Most Helpful This Week
Program in Go language to Find Largest Element of an Array
Golang program for implementation of AVL Trees
Convert Float32 to Float64 and Float64 to Float32
Golang write CSV records
Copy Struct Type Using Value and Pointer Reference
Cannot use <variable> (type <type>) as type <new-type> error in Golang