du131642 2011-09-14 23:53
浏览 72
已采纳

读入文本文件中的行,进行排序,然后覆盖文件

I am trying to write a go function that will read in lines in a text file, sort them (alphabetize), and overwrite them back to the file. Right now, I am able to essentially emulate cat, but I can't seem to be able to manipulate the contents of the elements in read_line.

func sort() {

    ff, _ := os.OpenFile(file, os.O_RDWR, 0666)
    f := bufio.NewReader(ff)
    for {
        read_line, _ := f.ReadString('
')
        fmt.Print(read_line)
        if read_line == "" {
            break
        }
    }
    ff.Close()
}

when i use ReadString, how can i store each line into a slice (or is there a better way to store them so i can manipulate them)? Then I would use the sort package in a manner similar to this:

sorted := sort.Strings(lines) 

then, to write to the file, i am using something similar to the following, although i have not included it because i have not yet gotten "sort" to work:

io.WriteString(ff, (lines + "
"))

Thank you in advance for any suggestions!

  • 写回答

4条回答 默认 最新

  • duanjiao6711 2011-09-15 02:39
    关注

    For example,

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
        "sort"
    )
    
    func readLines(file string) (lines []string, err os.Error) {
        f, err := os.Open(file)
        if err != nil {
            return nil, err
        }
        defer f.Close()
        r := bufio.NewReader(f)
        for {
            const delim = '
    '
            line, err := r.ReadString(delim)
            if err == nil || len(line) > 0 {
                if err != nil {
                    line += string(delim)
                }
                lines = append(lines, line)
            }
            if err != nil {
                if err == os.EOF {
                    break
                }
                return nil, err
            }
        }
        return lines, nil
    }
    
    func writeLines(file string, lines []string) (err os.Error) {
        f, err := os.Create(file)
        if err != nil {
            return err
        }
        defer f.Close()
        w := bufio.NewWriter(f)
        defer w.Flush()
        for _, line := range lines {
            _, err := w.WriteString(line)
            if err != nil {
                return err
            }
        }
        return nil
    }
    
    func main() {
        file := `lines.txt`
        lines, err := readLines(file)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
        sort.Strings(lines)
        err = writeLines(file, lines)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)