从预格式化的文件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.

dtn913117
dtn913117 我不知道是否有可能不在所有空间上分开。我还是把钱给了阿德里安。再次感谢。
2 年多之前 回复
dongyu5482
dongyu5482 不要使用fmt.Scan系列功能。它们不是为此设计的。
2 年多之前 回复
dongpo5239
dongpo5239 这就是为什么您不会在所有空间上均分的原因,只知道您所需要的空间即可。现在,如果您还想捕获具有多个参数的函数宏,它将变得更加复杂。
2 年多之前 回复
duansan9435
duansan9435 由于定义了宏。如果我按照空格分割事物,那么如果我定义了#define值形式“Thisisatest”,它将失败
2 年多之前 回复
dongliushui2001
dongliushui2001 语法是用空格分隔的,为什么不能只在空白处分割呢?
2 年多之前 回复
douque2016
douque2016 我也尝试过使用fmt.Fscan和fmt.Fscanln。他们每个人都有自己的问题。如果有人可以说服我说这些功能在这种情况下很有用,我将再试一次。
2 年多之前 回复

1个回答



我个人会这样做:</ p>

 文件,_:  = os.Open(“ preprocessorOutput.txt”)
br:= bufio.NewReader(file)
results = make(map [string] string)
for {
ln,_,err:= br.ReadLine()\ n //删除#define
ln = ln [8:]
//获取键值
个部分=字节。Splitn(ln,[] byte {''},2)
//保存
结果[string(parts [0])] = string(parts [1])

如果错误!= nil {
break
}
}
</ code> </ pre>
\ n

然后,您可以通过执行映射查找来回答与键关联的值的任何查询。 请注意,这很粗糙,没有错误检查,健全性检查或测试,但我希望它能提供一个不错的起点。</ p>
</ div>

展开原文

原文

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.

dtll2016
dtll2016 哦。 这更有意义。
2 年多之前 回复
dongzhanlian6289
dongzhanlian6289 我并不是说您不应该拥有它们,只是我没有将它们写入示例中。
2 年多之前 回复
dougangxin5695
dougangxin5695 我将尝试并确认。 虽然我来golang的原因是它的错误检查和健全性检查。 :(
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐