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 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统