12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- // Dup2 prints the count and text of lines that appear more than once
- // in the input. It reads from stdin or from a list of named files.
- package main
- import (
- "bufio"
- "fmt"
- "os"
- )
- func Test() {
- fmt.Printf("test message!")
- }
- func DUP2() {
- counts := make(map[string]int)
- files := os.Args[1:]
- for _,value := range files {
- f,err :=os.Open(value)
- fmt.Printf("内容:%s\n",value)
- fmt.Print(f,err) // 地址
- }
- if len(files) == 0 {
- countLines(os.Stdin, counts)
- } else {
- for _, arg := range files {
- f, err := os.Open(arg)
- if err != nil {
- fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
- continue
- }
- countLines(f, counts)
- f.Close()
- }
- }
- for line, n := range counts {
- fmt.Printf("%d\t%s\n", n, line)
- }
- }
- //这里是读的文件内容
- func countLines(f *os.File, counts map[string]int) {
- input := bufio.NewScanner(f)
- for input.Scan() && input.Text() != "end!" { //读取file的输入.--todo:这里的读取过程,会报错么,该怎么改
- counts[input.Text()]++
- }
- // NOTE: ignoring potential errors from input.Err()
- }
|