Golang Fscanf示例

说明

golang fscanf示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Golang

命名空间/包名称: fmt

示例#1
文件: 544.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("544.in")
	defer in.Close()
	out, _ := os.Create("544.out")
	defer out.Close()

	var kase, n, r, l int
	var c1, c2 string
	first := true
	for {
		if fmt.Fscanf(in, "%d%d", &n, &r); n == 0 && r == 0 {
			break
		}
		if first {
			first = false
		} else {
			fmt.Fprintln(out)
		}
		matrix := make(map[string]map[string]int)
		for r > 0 {
			fmt.Fscanf(in, "%s%s%d", &c1, &c2, &l)
			if _, ok := matrix[c1]; !ok {
				matrix[c1] = make(map[string]int)
			}
			if _, ok := matrix[c2]; !ok {
				matrix[c2] = make(map[string]int)
			}
			matrix[c1][c2], matrix[c2][c1] = l, l
			r--
		}
		fmt.Fscanf(in, "%s%s", &c1, &c2)
		kase++
		fmt.Fprintf(out, "Scenario #%d\n%d tons\n", kase, floydWarshall(matrix, c1, c2))
	}
}

示例#2
文件: 816.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("816.in")
	defer in.Close()
	out, _ := os.Create("816.out")
	defer out.Close()

	var n, f, token string
	var r1, c1, r2, c2 int
	var fm, to node
	for {
		if fmt.Fscanf(in, "%s", &n); n == "END" {
			break
		}
		fmt.Fscanf(in, "%d%d%s%d%d", &r1, &c1, &f, &r2, &c2)
		fm = node{r1, c1, toFace(f[0])}
		to = node{r2, c2, -1}
		maze = make(map[node][]node)
		for {
			if _, err := fmt.Fscanf(in, "%d%d", &r1, &c1); err != nil {
				break
			}
			var dir []string
			for {
				if fmt.Fscanf(in, "%s", &token); token == "*" {
					break
				}
				dir = append(dir, token)
			}
			buildMaze(r1, c1, dir)
		}
		p := bfs(fm, to)
		output(out, n, p)
	}
}

示例#3
文件: 429.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("429.in")
	defer in.Close()
	out, _ := os.Create("429.out")
	defer out.Close()

	var n int
	var word, fm, to string
	var dict map[string][]string
	fmt.Fscanf(in, "%d\n\n", &n)
	for i := 0; i < n; i++ {
		dict = make(map[string][]string)
		for {
			if fmt.Fscanf(in, "%s", &word); word == "*" {
				break
			}
			buildLink(dict, word)
		}
		for {
			if _, err := fmt.Fscanf(in, "%s%s\n", &fm, &to); err != nil {
				break
			}
			fmt.Fprintln(out, fm, to, bfs(dict, fm, to))
		}
	}
}

示例#4
文件: 340.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("340.in")
	defer in.Close()
	out, _ := os.Create("340.out")
	defer out.Close()

	var n, kase int
	for {
		if fmt.Fscanf(in, "%d", &n); n == 0 {
			break
		}
		kase++
		fmt.Fprintf(out, "Game %d:\n", kase)
		s, g := make([]int, n), make([]int, n)
		for i := range s {
			fmt.Fscanf(in, "%d", &s[i])
		}
		for {
			stop := true
			for i := range g {
				fmt.Fscanf(in, "%d", &g[i])
				if g[i] != 0 {
					stop = false
				}
			}
			if stop {
				break
			}
			strong, weak := match(s, g)
			fmt.Fprintf(out, "    (%d,%d)\n", strong, weak)
		}
	}
}

示例#5
文件: 642.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("642.in")
	defer in.Close()
	out, _ := os.Create("642.out")
	defer out.Close()

	var word string
	dict := make(map[string]map[byte]int)
	for {
		if fmt.Fscanf(in, "%s", &word); word == "XXXXXX" {
			break
		}
		dict[word] = split(word)
	}
	for {
		if fmt.Fscanf(in, "%s", &word); word == "XXXXXX" {
			break
		}
		if matched := find(word, dict); len(matched) == 0 {
			fmt.Fprintln(out, "NOT A VALID WORD")
		} else {
			fmt.Fprintln(out, strings.Join(matched, "\n"))
		}
		fmt.Fprintln(out, "******")
	}
}

示例#6
文件: 315.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("315.in")
	defer in.Close()
	out, _ := os.Create("315.out")
	defer out.Close()

	s := bufio.NewScanner(in)
	s.Split(bufio.ScanLines)

	var n, c1, c2 int
	var line string
	for s.Scan() {
		if n, _ = strconv.Atoi(s.Text()); n == 0 {
			break
		}
		matrix := make([][]bool, n)
		for i := range matrix {
			matrix[i] = make([]bool, n)
		}
		for s.Scan() {
			if line = s.Text(); line == "0" {
				break
			}
			r := strings.NewReader(line)
			fmt.Fscanf(r, "%d", &c1)
			for {
				if _, err := fmt.Fscanf(r, "%d", &c2); err != nil {
					break
				}
				matrix[c1-1][c2-1], matrix[c2-1][c1-1] = true, true
			}
		}
		fmt.Fprintln(out, solve(matrix))
	}
}

示例#7
文件: 574.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("574.in")
	defer in.Close()
	out, _ := os.Create("574.out")
	defer out.Close()

	var n int
	for {
		if fmt.Fscanf(in, "%d%d", &t, &n); t == 0 && n == 0 {
			break
		}
		nums = make([]int, n)
		for i := range nums {
			fmt.Fscanf(in, "%d", &nums[i])
		}
		answers = nil
		backtracking(0, 0, nil)
		fmt.Fprintf(out, "Sums of %d:\n", t)
		if len(answers) == 0 {
			fmt.Fprintln(out, "NONE")
		} else {
			for _, answer := range answers {
				fmt.Fprintln(out, strings.Join(answer, "+"))
			}
		}
	}
}

示例#8
文件: 10013.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("10013.in")
	defer in.Close()
	out, _ := os.Create("10013.out")
	defer out.Close()

	var kase, n int
	fmt.Fscanf(in, "%d", &kase)
	for kase > 0 {
		fmt.Fscanf(in, "\n%d", &n)
		n1 := make([]byte, n)
		n2 := make([]byte, n)
		for i := range n1 {
			fmt.Fscanf(in, "%d%d", &n1[i], &n2[i])
		}
		res := make([]byte, n)
		var carry bool
		for i := n - 1; i >= 0; i-- {
			sum := n1[i] + n2[i]
			if carry {
				sum++
			}
			carry = sum >= 10
			res[i] = sum % 10
		}
		for _, v := range res {
			fmt.Fprint(out, v)
		}
		fmt.Fprintln(out)
		kase--
	}
}

示例#9
文件: 10611.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("10611.in")
	defer in.Close()
	out, _ := os.Create("10611.out")
	defer out.Close()

	var n, q, height int
	fmt.Fscanf(in, "%d", &n)
	ladies := make([]int, n)
	for i := range ladies {
		fmt.Fscanf(in, "%d", &ladies[i])
	}
	fmt.Fscanf(in, "%d", &q)
	for q > 0 {
		fmt.Fscanf(in, "%d", &height)
		low, high := binarySearchLow(ladies, height), binarySearchHigh(ladies, height)
		if low != -1 {
			fmt.Fprint(out, low)
		} else {
			fmt.Fprint(out, "X")
		}
		if high != -1 {
			fmt.Fprintln(out, "", high)
		} else {
			fmt.Fprintln(out, " X")
		}
		q--
	}
}

示例#10
文件: cred.go 项目: therob3000/decloud

func verifyOcSig(reqHash []byte, ocID msg.OcID, sig string) bool {
	ocCredReader := strings.NewReader(ocID.String())
	var x, y, r, s big.Int
	n, err := fmt.Fscanf(ocCredReader, string(OC_ID_PREFIX)+"%x,%x", &x, &y)
	if err != nil {
		return false
	}
	n, err = ocCredReader.Read(make([]byte, 1))
	if n != 0 || err != io.EOF {
		return false
	}

	sigReader := strings.NewReader(sig)
	n, err = fmt.Fscanf(sigReader, "%x,%x", &r, &s)
	if err != nil {
		return false
	}
	n, err = sigReader.Read(make([]byte, 1))
	if n != 0 || err != io.EOF {
		return false
	}

	curve := elliptic.P256()
	pub := ecdsa.PublicKey{
		Curve: curve,
		X:     &x,
		Y:     &y,
	}
	return ecdsa.Verify(&pub, reqHash, &r, &s)
}

示例#11
文件: 10009.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("10009.in")
	defer in.Close()
	out, _ := os.Create("10009.out")
	defer out.Close()

	var kase, m, n int
	var fm, to string
	fmt.Fscanf(in, "%d\n", &kase)
	for kase > 0 {
		fmt.Fscanf(in, "\n%d%d", &m, &n)
		links = make(map[string][]string)
		for i := 0; i < m; i++ {
			fmt.Fscanf(in, "%s%s", &fm, &to)
			links[fm] = append(links[fm], to)
			links[to] = append(links[to], fm)
		}
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%s%s", &fm, &to)
			path := bfs(fm, to)
			for _, v := range path {
				fmt.Fprintf(out, "%c", v)
			}
			fmt.Fprintln(out)
		}
		if kase--; kase > 0 {
			fmt.Fprintln(out)
		}
	}
}

示例#12
文件: main.go 项目: dmikalova/practice

func parseFile(filename string) points {
	if filename == "" {
		log.Fatalf("No filename specified.")
	}

	// Open the file.
	fileReader, err := os.Open(filename)
	defer fileReader.Close()
	if err != nil {
		log.Fatalf("Error opening %s: %s.\n", filename, err)
	}

	var c uint
	n, err := fmt.Fscanf(fileReader, "%d\n", &c)
	if err != nil {
		log.Fatalf("Error scanning the first line. n: %d, err: %s.\n", n, err)
	}

	if c < 2 {
		log.Fatalf("Less than 2 points to compare. count: %d.\n", c)
	}

	p := make([]point, c)
	for i := 0; i < len(p); i++ {
		var x, y float64
		n, err := fmt.Fscanf(fileReader, "(%f,%f)\n", &x, &y)
		if err != nil {
			log.Fatalf("Error scanning line %d. n: %d, err: %s\n", i+1, n, err)
		}

		p[i] = point{x: x, y: y}
	}

	return p
}

示例#13
文件: 10000.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("10000.in")
	defer in.Close()
	out, _ := os.Create("10000.out")
	defer out.Close()

	var n, s, n1, n2, kase int
	for {
		if fmt.Fscanf(in, "%d", &n); n == 0 {
			break
		}
		fmt.Fscanf(in, "%d", &s)
		adj = make([][]bool, n+1)
		for i := range adj {
			adj[i] = make([]bool, n+1)
		}
		for {
			if fmt.Fscanf(in, "%d%d", &n1, &n2); n1 == 0 && n2 == 0 {
				break
			}
			adj[n1][n2] = true
		}
		distance = make([]int, n+1)
		longest = 0
		lowest = math.MaxInt32
		dfs(s)
		kase++
		fmt.Fprintf(out, "Case %d: The longest path from %d has length %d, finishing at %d\n\n", kase, s, longest, lowest)
	}
}

示例#14
文件: 378.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("378.in")
	defer in.Close()
	out, _ = os.Create("378.out")
	defer out.Close()

	var n int
	var x, y float64
	fmt.Fscanf(in, "%d", &n)
	fmt.Fprintln(out, "INTERSECTING LINES OUTPUT")
	for n > 0 {
		fmt.Fscanf(in, "%f%f", &x, &y)
		p1 := point{x, y}
		fmt.Fscanf(in, "%f%f", &x, &y)
		p2 := point{x, y}
		l1 := line{p1, p2}
		fmt.Fscanf(in, "%f%f", &x, &y)
		p1 = point{x, y}
		fmt.Fscanf(in, "%f%f", &x, &y)
		p2 = point{x, y}
		l2 := line{p1, p2}
		solve(l1, l2)
		n--
	}
	fmt.Fprintln(out, "END OF OUTPUT")
}

示例#15
文件: login.go 项目: WilliamRen/parse-cli

func (l *login) populateCreds(e *env) error {
	if l.credentials.email != "" && l.credentials.password != "" {
		return nil
	}

	fmt.Fprint(e.Out, "Email: ")
	fmt.Fscanf(e.In, "%s\n", &l.credentials.email)

	var (
		password string
		err      error
	)
	if e.In == os.Stdin {
		password, err = speakeasy.Ask("Password (will be hidden): ")
		if err != nil {
			return err
		}
	} else {
		// NOTE: only for testing
		fmt.Fscanf(e.In, "%s\n", &password)
	}

	if password != "" {
		l.credentials.password = password
	}
	return nil
}

示例#16
文件: template.go 项目: ctliu3/puzzles

func main() {
	// Read data.
	//r := bufio.NewReader(os.Stdin)

	// File operation.
	f, err := os.Open("in")
	if err != nil {
		return
	}
	defer f.Close()

	r := bufio.NewReader(f)
	w := bufio.NewWriter(os.Stdout)

	var n, val int
	fmt.Fscanf(r, "%d ", &n)

	var a []int
	for i := 0; i < n; i++ {
		fmt.Fscanf(r, " %d", &val)
		a = append(a, val)
	}

	// Output data.
	for _, val := range a {
		fmt.Fprintf(w, "%d ", val)
	}
	w.Flush()
}

示例#17
文件: 567.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("567.in")
	defer in.Close()
	out, _ := os.Create("567.out")
	defer out.Close()

	var cnt, tmp, set, kase, n1, n2 int
	for {
		matrix = make([][]bool, N+1)
		for i := range matrix {
			matrix[i] = make([]bool, N+1)
		}
		for i := 1; i < N; i++ {
			if _, err := fmt.Fscanf(in, "%d", &cnt); err != nil {
				return
			}
			for j := 0; j < cnt; j++ {
				fmt.Fscanf(in, "%d", &tmp)
				matrix[i][tmp] = true
				matrix[tmp][i] = true
			}
		}
		set++
		fmt.Fprintf(out, "Test Set #%d\n", set)
		fmt.Fscanf(in, "%d", &kase)
		for i := 0; i < kase; i++ {
			fmt.Fscanf(in, "%d%d", &n1, &n2)
			fmt.Fprintf(out, "%2d to %2d: %d\n", n1, n2, bfs(n1, n2))
		}
		fmt.Fprintln(out)
	}
}

示例#18
文件: 478.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("478.in")
	defer in.Close()
	out, _ = os.Create("478.out")
	defer out.Close()

	var shapes []shape
	var a, b, c, d, e, f float64
	var str string
	for {
		if fmt.Fscanf(in, "%s", &str); str == "*" {
			break
		}
		switch str {
		case "r":
			fmt.Fscanf(in, "%f%f%f%f", &a, &b, &c, &d)
			shapes = append(shapes, rectangle{point{a, b}, point{c, d}})
		case "c":
			fmt.Fscanf(in, "%f%f%f", &a, &b, &c)
			shapes = append(shapes, circle{point{a, b}, c})
		case "t":
			fmt.Fscanf(in, "%f%f%f%f%f%f", &a, &b, &c, &d, &e, &f)
			shapes = append(shapes, triangle{point{a, b}, point{c, d}, point{e, f}})
		}
	}

	count := 0
	for {
		if fmt.Fscanf(in, "%f%f", &a, &b); a == 9999.9 && b == 9999.9 {
			break
		}
		count++
		testIn(count, point{a, b}, shapes)
	}
}

示例#19
文件: 10443.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("10443.in")
	defer in.Close()
	out, _ = os.Create("10443.out")
	defer out.Close()

	var kase, r, c, n int
	var grid [][]byte
	var line string
	fmt.Fscanf(in, "%d", &kase)
	for kase > 0 {
		fmt.Fscanf(in, "%d%d%d", &r, &c, &n)
		grid = make([][]byte, r)
		for j := range grid {
			grid[j] = make([]byte, c)
			fmt.Fscanf(in, "%s", &line)
			for k := range line {
				grid[j][k] = line[k]
			}
		}
		solve(grid, r, c, n)
		output(grid)
		if kase--; kase > 0 {
			fmt.Fprintln(out)
		}
	}
}

示例#20
文件: 125.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("125.in")
	defer in.Close()
	out, _ := os.Create("125.out")
	defer out.Close()

	var n, n1, n2, kase int
	for {
		if _, err := fmt.Fscanf(in, "%d", &n); err != nil {
			break
		}
		var street [][2]int
		m = 0
		for n > 0 {
			fmt.Fscanf(in, "%d%d", &n1, &n2)
			street = append(street, [2]int{n1, n2})
			m = max(m, max(n1, n2))
			n--
		}
		fmt.Fprintf(out, "matrix for city %d\n", kase)
		kase++
		for _, vi := range solve(street) {
			fmt.Fprintln(out, strings.Join(vi, " "))
		}
	}
}

示例#21
文件: 10679.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("10679.in")
	defer in.Close()
	out, _ := os.Create("10679.out")
	defer out.Close()

	var kase, n int
	var s, t string
	fmt.Fscanf(in, "%d", &kase)
	for kase > 0 {
		fmt.Fscanf(in, "%s", &s)
		fmt.Fscanf(in, "%d", &n)
		for n > 0 {
			fmt.Fscanf(in, "%s", &t)
			// may need Aho–Corasick algorithm, but ah well
			if strings.Contains(s, t) {
				fmt.Fprintln(out, "y")
			} else {
				fmt.Fprintln(out, "n")
			}
			n--
		}
		kase--
	}
}

示例#22
文件: 10530.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("10530.in")
	defer in.Close()
	out, _ := os.Create("10530.out")
	defer out.Close()

	var n int
	l, h, cheating := 1, 10, false
	var s1, s2 string
	for {
		if fmt.Fscanf(in, "%d", &n); n == 0 {
			break
		}
		fmt.Fscanf(in, "%s%s", &s1, &s2)
		switch {
		case s2 == "high":
			h = n - 1
		case s2 == "low":
			l = n + 1
		default:
			if cheating || n < l || n > h {
				fmt.Fprintln(out, "Stan is dishonest")
			} else {
				fmt.Fprintln(out, "Stan may be honest")
			}
			l, h, cheating = 1, 10, false
		}
		if h < l {
			cheating = true
		}
	}
}

示例#23
文件: 414.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("414.in")
	defer in.Close()
	out, _ := os.Create("414.out")
	defer out.Close()

	var n int
	var ch byte
	for {
		if fmt.Fscanf(in, "%d", &n); n == 0 {
			break
		}
		nums := make([]int, n)
		max := 0
		for n > 0 {
			num := 0
			for i := 1; i <= 25; i++ {
				if fmt.Fscanf(in, "%c", &ch); ch == 'X' {
					num++
				}
			}
			fmt.Fscanln(in)
			if num > max {
				max = num
			}
			nums[n-1] = num
			n--
		}
		total := 0
		for _, v := range nums {
			total += max - v
		}
		fmt.Fprintln(out, total)
	}
}

示例#24
文件: 10099.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("10099.in")
	defer in.Close()
	out, _ := os.Create("10099.out")
	defer out.Close()

	var n, r, c1, c2, p, s, d, t, kase int
	for {
		if fmt.Fscanf(in, "%d%d", &n, &r); n == 0 {
			break
		}
		matrix := make([][]int, n+1)
		for i := range matrix {
			matrix[i] = make([]int, n+1)
		}
		for r > 0 {
			fmt.Fscanf(in, "%d%d%d", &c1, &c2, &p)
			matrix[c1][c2], matrix[c2][c1] = p, p
			r--
		}
		fmt.Fscanf(in, "%d%d%d", &s, &d, &t)
		floydWarshall(n, matrix)
		trips := t / (matrix[s][d] - 1)
		if t%trips != 0 {
			trips++
		}
		kase++
		fmt.Fprintf(out, "Scenario #%d\n", kase)
		fmt.Fprintf(out, "Minimum Number of Trips = %d\n", trips)
	}
}

示例#25
文件: 10042.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("10042.in")
	defer in.Close()
	out, _ := os.Create("10042.out")
	defer out.Close()

	var n, num int
	fmt.Fscanf(in, "%d", &n)
	for n > 0 {
		fmt.Fscanf(in, "%d", &num)
		for {
			sum1 := digitSum(num)
			sum2 := 0
			for _, v := range primeFactorize(num) {
				sum2 += digitSum(v)
			}
			if sum1 == sum2 {
				fmt.Fprintln(out, num)
				break
			}
			num++
		}
		n--
	}
}

示例#26
文件: 352.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("352.in")
	defer in.Close()
	out, _ := os.Create("352.out")
	defer out.Close()

	var d, count int
	var m [][]byte
	var line string
	for {
		if _, err := fmt.Fscanf(in, "%d", &d); err != nil {
			break
		}
		count++
		m = make([][]byte, d)
		for i := range m {
			m[i] = make([]byte, d)
			fmt.Fscanf(in, "%s", &line)
			for j := range m[i] {
				m[i][j] = line[j]
			}
		}
		fmt.Fprintf(out, "Image number %d contains %d war eagles.\n", count, floodFill(m))
	}
}

示例#27
文件: 10033.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("10033.in")
	defer in.Close()
	out, _ := os.Create("10033.out")
	defer out.Close()

	var kase int
	var line string
	first := true
	fmt.Fscanf(in, "%d", &kase)
	for kase > 0 {
		if first {
			fmt.Fscanln(in)
			first = false
		} else {
			fmt.Fprintln(out)
		}
		var lines []string
		for {
			if _, err := fmt.Fscanf(in, "%s", &line); err != nil {
				break
			}
			lines = append(lines, line)
		}
		fmt.Fprintln(out, solve(lines))
		kase--
	}
}

示例#28
文件: 455.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("455.in")
	defer in.Close()
	out, _ := os.Create("455.out")
	defer out.Close()

	var n int
	var line string
	first := true
	fmt.Fscanf(in, "%d", &n)
	for n > 0 {
		n--
		if first {
			first = false
		} else {
			fmt.Fprintln(out)
		}
		fmt.Fscanln(in)
		fmt.Fscanf(in, "%s", &line)
		ks := factors(len(line))
		for _, k := range ks {
			if periodic(line, k) {
				fmt.Fprintln(out, k)
				break
			}
		}
	}
}

示例#29
文件: 476.go 项目: codingsince1985/UVa

func main() {
	in, _ := os.Open("476.in")
	defer in.Close()
	out, _ = os.Create("476.out")
	defer out.Close()

	var rectangles []rectangle
	var a, b, c, d float64
	var str string
	for {
		if fmt.Fscanf(in, "%s", &str); str == "*" {
			break
		}
		fmt.Fscanf(in, "%f%f%f%f", &a, &b, &c, &d)
		rectangles = append(rectangles, rectangle{point{a, b}, point{c, d}})
	}

	count := 0
	for {
		if fmt.Fscanf(in, "%f%f", &a, &b); a == 9999.9 && b == 9999.9 {
			break
		}
		count++
		testIn(count, point{a, b}, rectangles)
	}
}

示例#30
文件: io.go 项目: RenatoGeh/gospn

// Parse reads a vocabulary file vfile and returns an in-memory representation of it (Vocabulary).
func Parse(vfile string) *Vocabulary {
	voc, err := os.Open(io.GetPath(vfile))

	if err != nil {
		fmt.Printf("Error. Could not open file [%s].\n", vfile)
		panic(err)
	}
	defer voc.Close()

	var n int
	fmt.Fscanf(voc, "%d", &n)

	entries := make([]string, n)
	for i := 0; i < n; i++ {
		var j int
		var str string
		fmt.Fscanf(voc, "%d %s ", &j, &str)
		entries[j] = str
	}

	var m int
	fmt.Fscanf(voc, "%d", &m)

	l, block := 0, make([]int, m)
	for i := 0; i < m; i++ {
		var k int
		fmt.Fscanf(voc, "%d", &k)
		block[l] = k
		l++
	}

	return NewVocabulary(entries, block)
}

展开阅读全文