zoukankan      html  css  js  c++  java
  • 5.6 seeking a position within a file 查找文件中的位置

    
    package main
    
    import (
    	"errors"
    	"fmt"
    	"os"
    )
    
    const lineLegth = 25
    
    func main() {
    
    	f, e := os.OpenFile("flatfile.txt", os.O_RDWR|os.O_CREATE, os.ModePerm)
    	if e != nil {
    		panic(e)
    	}
    	defer f.Close()
    
    	fmt.Println(readRecords(2, "last", f))
    	if err := writeRecord(2, "first", "Radomir", f); err != nil {
    		panic(err)
    	}
    	fmt.Println(readRecords(2, "first", f))
    	if err := writeRecord(10, "first", "Andrew", f); err != nil {
    		panic(err)
    	}
    	fmt.Println(readRecords(10, "first", f))
    	fmt.Println(readLine(2, f))
    }
    
    func readLine(line int, f *os.File) (string, error) {
    	lineBuffer := make([]byte, 24)
    	f.Seek(int64(line*lineLegth), 0)
    	_, err := f.Read(lineBuffer)
    	return string(lineBuffer), err
    }
    
    func writeRecord(line int, column, dataStr string, f *os.File) error {
    	definedLen := 10
    	position := int64(line * lineLegth)
    	switch column {
    	case "id":
    		definedLen = 4
    	case "first":
    		position += 4
    	case "last":
    		position += 14
    	default:
    		return errors.New("Column not defined")
    	}
    
    	if len([]byte(dataStr)) > definedLen {
    		return fmt.Errorf("Maximum length for '%s' is %d", column, definedLen)
    	}
    
    	data := make([]byte, definedLen)
    	for i := range data {
    		data[i] = '.'
    	}
    	copy(data, []byte(dataStr))
    	_, err := f.WriteAt(data, position)
    	return err
    }
    
    func readRecords(line int, column string, f *os.File) (string, error) {
    	lineBuffer := make([]byte, 24)
    	f.ReadAt(lineBuffer, int64(line*lineLegth))
    	var retVal string
    	switch column {
    	case "id":
    		return string(lineBuffer[:3]), nil
    	case "first":
    		return string(lineBuffer[4:13]), nil
    	case "last":
    		return string(lineBuffer[14:23]), nil
    	}
    
    	return retVal, errors.New("Column not defined")
    }
    
    /*
     322e 2e4 <nil>
    Radomir.. <nil>
    Andrew... <nil>
    2e2eRadomir... 322e 2e4e <nil>
     */
    
     
     /* file content
     3132 332e 4a75 6e2e 2e2e 2e2e 2e2e 576f
    6e67 2e2e 2e2eRadomir... 322e 2e4e 6f76
    616b 2e2e 2e2e 2e4a 7572 6765 6e2e 2e2e
    2e0a 3130 2e2e 5261 646f 6d69 722e 2e2e
    536f 686c 6963 682e 2e2e 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000Andrew.... 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 416e
    6472 6577 2e2e 2e2e
     
     
     */
    
  • 相关阅读:
    浏览器是怎样工作的二:渲染引擎 HTML解析(1)(转)
    凯文.都迪的超级记忆力训练教程
    程序员的修炼之道
    我编程我快乐——程序员的职业规划
    SQL Server 数据库备份和还原认识和总结(一)
    SQL Server 管理数据收集
    汇总SQL Server里的相关运算符、子句、谓词等
    SQL Server 数据库备份和还原认识和总结(二)
    解决报表控件报CS0433错误
    通过笔记本配件,理解抽象类接口和委托事件
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8620959.html
Copyright © 2011-2022 走看看