免费注册 查看新帖 |

Chinaunix

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

reformat data file [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-03-31 04:47 |只看该作者 |倒序浏览
小弟有一个文本格式如下:

1      -32      34     12        23     -445     212     32     23          4
2       32      2       23     -445      212       32     23       2     -234
. . .
. . .

然后我想写一个c 或者fortran程序把这些数字按照下面的格式输出:
1      
-32
34     
12        
23     
-445     
212     
32     
23         
4
2      
32      
2      
23     
-445     
212      
32     
23      
2     
-234
.
.
.
我自己写了一个fortran,但是一运行就出错,不知道用c怎么写,哪位朋友帮帮忙,谢谢! 我自己的fortran程序如下:

Program transform
implicit none
integer :: A
dimension :: A(10)
integer :: I
open(4,file="old.dat",status="unknown"
do I=1,10
read(4,100) A(I),A(I+1),A(I+2),A(I+3),A(I+4),A(I+5),A(I+6),A(I+7),A(I+,A(I+9)
end do
100 format(I7,I7,I7,I7,I7,I7,I7,I7,I7,I7)
open(5,file="new.dat",status="unknown"
do I=1,10
write (5,200) A(I)
end do
200 format(I7)
close(4)
close(5)
end

论坛徽章:
0
2 [报告]
发表于 2004-03-31 08:36 |只看该作者

reformat data file

参考一下下面的代码

  1. #include <stdio.h>;
  2. int main()
  3. {
  4.         FILE *fp=fopen("test.txt","r");/*test.txt里是你的数据,我看到似乎是十个一组的*/
  5.         if(fp!=NULL)
  6.         {
  7.                 char buff[256];
  8.                 int data[10];
  9.                 int m=0;
  10.                 while(fgets(buff,256,fp)>;0)
  11.                 {
  12.                        
  13.                         char end[4];
  14.                         char *p=strtok(buff," ");
  15.                         while((p!=NULL)&&(m<10))
  16.                         {
  17.                                 data[m++]=atoi(p);
  18.                                 p=strtok(p+strlen(p)+1);
  19.                         }
  20.                         while(m-->;0)
  21.                         printf("data[%d]=%d\n",m,data[m]);
  22.                 }
  23.                
  24.                 fclose(fp);
  25.         }
  26. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2004-03-31 08:39 |只看该作者

reformat data file

上面的输出是倒着的,省事了一点,望见谅。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP