golang逐行处理文件

 golang 提供了package bufio。bufio.NewReader()创建一个默认大小的readbuf,当然,也可以bufio.NewReaderSize

func NewReader(rd io.Reader) *Reader
    NewReader returns a new Reader whose buffer has the default size(4096).


func NewReaderSize(rd io.Reader, size int) *Reader
    NewReaderSize returns a new Reader whose buffer has at least the
    specified size. If the argument io.Reader is already a Reader with large
    enough size, it returns the underlying Reader.

bufio

func (b *Reader) ReadByte() (c byte, err error)
    ReadByte reads and returns a single byte. If no byte is available,
    returns an error.

func (b *Reader) ReadBytes(delim byte) (line []byte, err error)
    ReadBytes reads until the first occurrence of delim in the input,
    returning a slice containing the data up to and including the delimiter.
    If ReadBytes encounters an error before finding a delimiter, it returns
    the data read before the error and the error itself (often io.EOF).
    ReadBytes returns err != nil if and only if the returned data does not
    end in delim. For simple uses, a Scanner may be more convenient.

func (b *Reader) ReadString(delim byte) (line string, err error)
    ReadString reads until the first occurrence of delim in the input,
    returning a string containing the data up to and including the
    delimiter. If ReadString encounters an error before finding a delimiter,
    it returns the data read before the error and the error itself (often
    io.EOF). ReadString returns err != nil if and only if the returned data
    does not end in delim. For simple uses, a Scanner may be more
    convenient.

ReadByte这个接口,和C语言中fgetc很接近,每次读取一个字节。ReadBytes和ReadString都可以实现逐行读取,只要delim设置为'\n'.

package main
import "fmt"
import "os"
import "io"
import "flag"
import "bufio"

var num_flag = flag.Bool("n",false,"num each line")

func usage(){
    fmt.Printf("%s %s\n",os.Args[0],"filename")
}



func cat(r *bufio.Reader){
    i := 1
    for {
        //buf,err := r.ReadBytes('\n')
        buf,err := r.ReadString('\n')
        if err == io.EOF{
            break
        }

        if *num_flag{
            fmt.Fprintf(os.Stdout,"%5d %s",
                        i,buf)
            i++
        }else{
            fmt.Fprintf(os.Stdout,"%s",buf)
        }

    }
    return 
}


func main(){

    flag.Parse()
    if(flag.NArg() == 0){
        cat(bufio.NewReader(os.Stdin))
    }

    for i:=0;i<flag.NArg();i++{
        f,err := os.OpenFile(flag.Arg(i),os.O_RDONLY,0660)
        if err != nil{
            fmt.Fprintf(os.Stderr,"%s err read from %s : %s\n",
            os.Args[0],flag.Arg(0),err)
            continue
        }

        cat(bufio.NewReader(f))
        f.Close()
    }
}

用scaner逐行读取

func cat(scanner *bufio.Scanner) error{

    for scanner.Scan(){
        fmt.Println(scanner.Text())    
      //fmt.Fprintf(os.Stdout,"%s\n",scanner.Text())
    }

    return scanner.Err()
}

注意,为啥执行Scan,Text()函数就能返回下一行呢?因为默认的分割函数就是ScanLines.如你有特殊的需求来分割,func (s *Scanner) Split(split SplitFunc)

这个函数可以制定SplitFunc。你可以定制自己的分割函数。

    需要注意的是,Scan会将分割符号\n去除,如果Fprintf输出的话,不添加\n打印,会出现没有换行的现象,如下所示

fmt.Fprintf(os.Stdout,"%s",scanner.Text())
manu@manu-hacks:~/code/go/self$ go run mycat_v2.go test.txt 
this is test file created by goif not existed ,please create this fileif existed, Please write appendhello world,hello gothis is test file created by goif not existed ,please create this fileif existed, Please write appendhello world,hello gomanu@manu-hacks:~/code/go/self$ cat test.txt 
this is test file created by go
if not existed ,please create this file
if existed, Please write append
hello world,hello go
this is test file created by go
if not existed ,please create this file
if existed, Please write append
hello world,hello go

  调用部分的代码如下:

 f,err := os.OpenFile(flag.Arg(i),os.O_RDONLY,0660)
                 ...
        error := cat(bufio.NewScanner(f))
        if err != nil{
            fmt.Fprintf(os.Stderr,"%s err read from %s : %s\n",
            os.Args[0],flag.Arg(i),error)
        }


转载于:https://my.oschina.net/lengxugz/blog/497042

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值