doufei7516 2018-06-22 18:23
浏览 53
已采纳

从预格式化的文件Golang中读取

I am trying to write a golang program which will be able to read from a file of the following type of data

#define __LPM_classic__(addr) (__extension__({ uint16_t __addr16 = (uint16_t)(addr); uint8_t __result; __asm__ __volatile__ ( "lpm" "
\t" "mov %0, r0" "
\t" : "=r" (__result) : "z" (__addr16) : "r0" ); __result; }))
#define PRIXFAST32 "lX"
#define INT0 0
#define INT1 1
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define B00010000 16
#define B11101000 232
#define B11101001 233
#define PRADC 0
#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
#define Arduino_h 
#define sq(x) ((x)*(x))
#define B01000100 68
#define B01000101 69

(For anyone wondering, yes this is an output from a Clang pre-processor. The reasons for me doing this is pretty complicated, and I don't want to trouble people with un-necessary details)

I need to write a golang program, which will read through this file, and give me the defined value of a string. For example

func getDefinedValue(filepath string,defineName string) string{
    //Need help with this part
}

So if I run the function in the following manner

Value :=getDefinedValue("preprocessorOutput.txt","PRADC")

Then the variable value should hold 0, as per the example file above.

I have tried using fmt.Fscnaf in the following manner

file, err := os.Open("preprocessorOutput.txt")
Value :=""
readLine :=""
defName :=""
var errr error
for (errr == nil) && (len(Value)==0){
    ret,err :=fmt.Fscanf(file,"#define %s %s",&defName,&readLine)
    errr=err
    fmt.Println("This is returned ",ret, " and this is the defName ",defName, " And this is the value ",readLine," and this is the error",err)
    if(ret <1){
        continue
    }
    //I planned to process the defName and readline to get the actual value here, as per which defName I want to get.
}

(Don't sweat the details, this is my attempt at an MCVE) which allows me to use a format string like CLang's scanf allows, but it fails on the 1st line of the example and throws an error on the 2nd line.

This is my first-day using golang, and I would probably kill to have a package do this work for me.

  • 写回答

1条回答 默认 最新

  • dscrb00804 2018-06-22 18:41
    关注

    Personally, I'd do something like this:

    file, _ := os.Open("preprocessorOutput.txt")
    br := bufio.NewReader(file)
    results = make(map[string]string)
    for {
        ln, _, err := br.ReadLine()
        // Trim off #define
        ln = ln[8:]
        // Get key value
        parts = bytes.Splitn(ln, []byte{' '}, 2)
        // Save
        results[string(parts[0])] = string(parts[1])
    
        if err != nil {
            break
        }
    }
    

    Then you can answer any query for the value associated with a key by doing a map lookup. Note that this is very rough with no error checking, sanity checking, or indeed testing, but I hope it gives a decent starting point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度