| 查看: 1241 | 回复: 5 | |||
[交流]
【求助】菜鸟求助c语言读文件小问题
|
|
2 UGC12889 0.00047 47.27450 3.1 13.31 1.546 0.498 1.314 ~ 0.85 0.100 ~ -21.05 72.458 10.869 0.61 0.61 4 PGC000004 0.00096 23.08764 5.0 15.39 0.851 0.078 0.186 ~ 0.219 0.015 ~ -18.68 63.264 13.918 0.39 0.40 6 PGC000006 0.00058 15.88165 -1.0 15.23 0.457 0.169 0.324 ~ 0.708 0.082 ~ -19.46 84.181 18.520 0.34 0.35 7 PGC000007 0.00122 -0.08326 -3.2 15.54 0.575 0.093 0.467 ~ 0.813 0.056 ~ -19.46 97.347 21.416 0.33 0.34 10 PGC000010 0.00217 -0.04057 -3.2 15.56 0.562 0.078 0.446 ~ 0.794 0.037 ~ -19.46 98.250 21.615 0.29 0.31 12 PGC000012 0.00240 -6.37390 1.1 14.05 1.045 0.336 0.199 ~ 0.19 0.022 ~ -20.79 92.153 13.823 0.36 0.37 txt文件,格式如上,空格是分隔符。读出期中六列分辨存为数组。 我写的程序如下: 提示 memory error22 (就是扩展的时候出错了。)。请好心人帮帮忙。 #include #include #include #define BUF 1024 #define pi 3.14159 /* The function reads stings from a txt file. Here, we read GWGCCatalog.txt.(see main function) */ int readevent( FILE *pFile, int **pgc, char **name, double **rag, double **decg, double **absmag, double **dist) { void *pgc1, *name1, *rag1, *decg1, *absmag1, *dist1; char *temp, *temp1; /* these two variables can be overwritten. */ int i=0, n=1024; /* n is the buffer. */ if ( !(*pgc=(int *)malloc(n*sizeof(int)))|| !(*name=(char *)malloc(n*100))|| !(*rag=(double *)malloc(n*sizeof(double)))|| !(*decg=(double *)malloc(n*sizeof(double)))|| !(*absmag=(double *)malloc(n*sizeof(double)))|| !(*dist=(double *)malloc(n*sizeof(double)))|| !(temp=(char *)malloc(n*100)) ) /* assume the longth of all strings less than 300 */ printf("memory error11\n" ;while (!feof(pFile)) { fscanf(pFile, "%d %s %e %e %s %s %s %s %s %s %s %s %s %e %e %s %s %s", *pgc+i, *name+i*100, *rag+i, *decg+i, temp, temp, temp, temp, temp, temp, temp, temp, temp, *absmag+i, *dist+i, temp, temp, temp); i++; if (i==(n-1)) /*the number of lines is going to be longer than buffer. */ { if(!(pgc1=realloc(*pgc, 2*n*sizeof(int)))|| !(name1=realloc(*name, 2*n*100))|| !(rag1=realloc(*rag, 2*n*sizeof(double)))|| !(decg1=realloc(*decg, 2*n*sizeof(double)))|| !(absmag1=realloc(*absmag, 2*n*sizeof(double)))|| !(dist1=realloc(*dist, 2*n*sizeof(double)))|| !(temp1=realloc(temp, 2*n*100))) { printf("memory error22\n" ; }*pgc=pgc1; *name=name1; *rag=rag1; *decg=decg1; *absmag=absmag1; *dist=dist1; temp=temp1; n*=2; } /*double the buffer*/ } free(temp); return i; /* Returning i tell us the accurate number.*/ } int main() { int i, n; /* n is the number of lines in GWGCCatalog.txt. n1 is that in skymap.txt */ int *pgc; char *name=NULL; double *rag, *decg, *absmag, *dist; FILE *pFile, *out; if ( !( pFile=fopen("GWGCCatalog.txt", "r" )) {printf ("cannot open the GWGCCatalog.txt file\n" ;return 1;} if ( !( out=fopen("goutput.txt", "w" )) {printf ("cannot open the goutput.txt file\n" ;return 1;} n=readevent(pFile, &pgc, &name, &rag, &decg, &absmag, &dist); for (i=0; i free(pgc); free(name); free(rag); free(decg); free(absmag); free(dist); fclose(pFile); fclose(out); } |
» 猜你喜欢
本科双非材料,跨考一志愿华电085801电气,283求调剂,任何专业都可以
已经有6人回复
材料与化工272求调剂
已经有9人回复
085602 化工专硕 338分 求调剂
已经有11人回复
070305高分子化学与物理 304分求调剂
已经有4人回复
一志愿北化085600材料专硕275|有文章专利|求调剂
已经有5人回复
一志愿北京工业大学,324分求调剂
已经有4人回复
320分,材料与化工专业,求调剂
已经有13人回复
学硕274求调剂
已经有7人回复
085600,材料与化工321分求调剂
已经有9人回复
求调剂
已经有4人回复
» 本主题相关价值贴推荐,对您同样有帮助:
C语言变量问题,求助大侠!!
已经有17人回复
c语言高手请进,请勿灌水,谢谢!(关于读写文件的问题)
已经有6人回复
【求助】菜鸟问题 输入流
已经有12人回复
在网上求助计算化学问题时的注意事项-Sobereva
已经有6人回复
【求助】菜鸟求助 matlab 近红外光谱小波变换问题
已经有10人回复
【求助】求一个关于文件读写的c语言小程序
已经有3人回复
【求助】VC++.NET编程,MFC基于对话框应用程序编写
已经有18人回复
» 抢金币啦!回帖就可以得到:
武汉征男友
+1/270
【招聘】温州医科大学/附属医院/瓯江实验室李校堃院士团队诚聘博士后和科研人员
+1/193
双一流南京医科大学招计算机、生物信息等方向调剂
+1/95
江南大学招收具有3年海外博后经历的青年教师
+1/80
青岛农业大学海洋学院招调剂硕士研究生
+1/73
中原食品实验室 & 郑州轻工业大学2026年硕士研究生联合培养调剂公告
+1/39
武汉纺织大学全重硕士调剂
+1/28
【实战型】【生物医药】2026青岛大学招博士生 含少数民族骨干计划2名!
+1/17
08开头有调剂需求的同学
+3/16
中国科学院上海硅酸盐研究所施剑林院士团队胡萍课题组招聘博士后
+1/14
长江大学石油工程学院2026年硕士调剂招生公告!!!
+1/13
西安工程大学能源化工新材料研发项目组接收2026年计算机类调剂研究生
+1/13
【博士招生】武汉科技大学招收材料、化工、环境、冶金类2026年“申请-考核”制博士生
+1/8
中国科学院上海硅酸盐研究所招聘科研助理(纳米材料与生物应用方向)
+1/7
考研292求调剂
+1/7
各种IgG抗体表达系统
+1/3
中国科学技术大学纳米科学技术学院张振教授/江雷院士诚招2026级博士
+1/3
山东济南省属重点高校生态环境类硕士调剂信息
+1/2
博士招生 | 青岛科技大学(高分子、化学、材料、力学、航天、机电工程、计算机仿真)
+1/1
教育部重点实验室招收2026年调剂生,化学、材料、机械、计算机等
+1/1
2楼2011-02-24 11:47:13
3楼2011-02-24 12:09:25
4楼2011-02-24 14:27:42
5楼2011-02-24 14:28:31
★ ★ ★ ★ ★
余泽成(金币+5): 谢谢参与应助! 2011-02-27 15:06:14
nenyan(金币+5): 不错不错 2011-02-28 07:35:09
余泽成(金币+5): 谢谢参与应助! 2011-02-27 15:06:14
nenyan(金币+5): 不错不错 2011-02-28 07:35:09
|
呵呵,不麻烦。比如你上面的数据可以用bash shell 很容易写成mathematica的输入形式 { {1,2,3,4,5,6,7,……}, {1,2,3,4,5,6,7,……}, . . . . {1,2,3,4,5,6,7,……}, {1,2,3,4,5,6,7,……} } 然后用mathematica的Get来读入,mathematica有一大堆功能强大的函数来处理数据,非常高效,呵呵。 比如你的数据文件名为urfile 1)##以下命令行让数据以逗号为间隔,存成urfile_for_Ma cat urfile |awk '{$1=$1;print}'|sed 's/ /,/g' >urfile_for_Ma 2)##以下命令给数据行添加{} sed -i 's/^/{/' urfile_for_Ma #给所有数据行首添加{ sed -i 's/$/},/' urfile_for_Ma #数据行末尾添加}, sed -i '1s/^/{/' urfile_for_Ma #第一行首再加个{ sed -i '$s/,$/}/' urfile_for_Ma #最后一行行尾的“,”换成“}” 搞定。可以用mathematica来处理了。 p.s.: 当然可以把这些命令放一个脚本文件里比如叫form_change,执行一下就ok了. 你还可以在脚本里定义变量 urfile=$1 然后再调用$urfile这个变量,这样你就可以用form_change urfile来执行,不用每次处理不同的文件都要改脚本文件里的文件名,省事很多 [ Last edited by bitgreen on 2011-2-25 at 05:17 ] |
6楼2011-02-25 05:16:26














;
回复此楼