• 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
     
     
     */
    
  • 相关阅读:
    遥控器油门摇杆电位器封装尺寸图
    Microhard P900 900MHz跳频电台核心模块
    航路点
    当电桥为恒流源时惠斯通电桥电压的计算方法
    曲轴位置传感器
    16种发动机动态工作原理图,神奇的帅呆了!
    ffmpeg mediacodec 硬解初探
    ffmpeg编码常见问题排查方法
    阿里云 访问控制RAM
    WannaCry勒索病毒处理指南
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8620959.html
Copyright © 2020-2023  润新知