免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1834 | 回复: 2
打印 上一主题 下一主题

perl读写文字和数据混杂的文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-29 11:37 |只看该作者 |倒序浏览
perl读写文字和数据混杂的文件, 有一文件vFloors,内容如下:
~v varFloor1
<Variance> 39
2.882107e-001 1.011243e+000 6.391264e-001 1.449148e+000 8.005224e-001
我希望把它读到另外一个文件 macros


脚本如下,发现只能读文字,数据不能读入,该如何解决?
#!/usr/local/bin/perl -w
use strict;

my ($macros, $vFloors,@labs);

# this script makes an mlf out of a list of file names and
# corresponding prompts - ie in the format
# fileid prompt
# fileid prompt
# "        "
# The prompts are automatically converted to upper case.


if (@ARGV != 2) {
  print "usage: $0 mlf promptlist\n\n";
  exit (0);
}

# read in command line arguments
($macros, $vFloors) = @ARGV;


# open MLF file
open (MACROS,">$macros") || die ("Unable to open mlf $macros file for writing");

print "writing to mlf file $macros\n";

print MACROS ("\~o\n");
print MACROS ("\<VECSIZE\> 39\<MFCC_0_D_A\>\n");
# open vFloors file
open (LAB, "$vFloors") || die ("Unable to open vFloors file $vFloors ");
#peint LAB pack("h8",99);  
binmode LAB;
while (@labs = <LAB>) {
  printf(MACROS "%s\n",@labs);
}
close (LAB);
close(MACROS);
print "writing to $macros file done\n";

论坛徽章:
0
2 [报告]
发表于 2009-12-29 11:49 |只看该作者

回复 #1 ironbridge 的帖子

已经解决,应该一行一行读入,请大家指教。我重新贴下,
#!/usr/local/bin/perl -w
use strict;

my ($macros, $vFloors,$labs);

# this script makes an mlf out of a list of file names and
# corresponding prompts - ie in the format
# fileid prompt
# fileid prompt
# "        "
# The prompts are automatically converted to upper case.


if (@ARGV != 2) {
  print "usage: $0 mlf promptlist\n\n";
  exit (0);
}

# read in command line arguments
($macros, $vFloors) = @ARGV;


# open MLF file
open (MACROS,">$macros") || die ("Unable to open mlf $macros file for writing");

print "writing to mlf file $macros\n";

print MACROS ("\~o\n");
print MACROS ("\<VECSIZE\> 39\<MFCC_0_D_A\>\n");
# open vFloors file
open (LAB, "$vFloors") || die ("Unable to open vFloors file $vFloors ");

while ($labs = <LAB>) {
  
  print MACROS $labs;
}
close (LAB);
close(MACROS);
print "writing to $macros file done\n";

论坛徽章:
0
3 [报告]
发表于 2009-12-29 16:13 |只看该作者
一次读入所有文件,不算什么错,但是,printf用的实在是不对,且不好:
printf(MACROS "%s\n",@labs);
这里没有必要用 printf,看上去很别扭,也容易出错:
1. @labs是所有的行,包括了回车符,没有必要多一个 \n
2. %s需要一个字串,但你的 @labs 提供了多个字串,所以后面的数据一个都没有被写入进去

所以,你改为 行读入然后用print ,问题自然没了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP