duanjingwei7239 2013-08-16 18:28
浏览 72
已采纳

开始-尝试创建.pgm文件,但最终得到一个二进制文件

I'm reading a pgm-file in order to apply some filter to it and rewrite the result to a new pgm-file. However, I always end up with a binary file (vim doesn't show the values but stuff like ^G ^0 ^K etc, the file-command tells me it's a data file and imagemagick can't open the file)

The code where I write to the file looks like this:

fd,err := os.Create(filename)
wr := bufio.NewWriter(fd)
//img is of type [][]int and holds the values to be written
str := "P2
" + filename + "
" + string(len(img[0])) + ...
if _,err := wr.WriteString(outStr); err != nil {..}

and then some more of that in a loop where I go through the img array.

What am I missing here?

  • 写回答

1条回答 默认 最新

  • dongque5529 2013-08-16 18:45
    关注

    string(len(img[0])) doesn't do what you're trying to do here. You need strconv.Itoa

    string(len(img[0])) is creating a single character string containing whichever character is the Unicode code point for len(img[0]) (see the spec section on conversions)

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?