| 查看: 1231 | 回复: 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); } |
» 猜你喜欢
材料调剂
已经有9人回复
286求调剂
已经有12人回复
281求调剂
已经有5人回复
化学工程085602 305分求调剂
已经有17人回复
287求调剂
已经有3人回复
一志愿211院校 344分 东北农业大学生物学学硕,求调剂
已经有3人回复
071000生物学求调剂,初试成绩343
已经有3人回复
一志愿 西北大学 ,070300化学学硕,总分287,双非一本,求调剂。
已经有6人回复
0703化学/290求调剂/本科经历丰富/工科也可
已经有7人回复
266分求材料化工冶金矿业等专业的调剂
已经有3人回复
» 本主题相关价值贴推荐,对您同样有帮助:
C语言变量问题,求助大侠!!
已经有17人回复
c语言高手请进,请勿灌水,谢谢!(关于读写文件的问题)
已经有6人回复
【求助】菜鸟问题 输入流
已经有12人回复
在网上求助计算化学问题时的注意事项-Sobereva
已经有6人回复
【求助】菜鸟求助 matlab 近红外光谱小波变换问题
已经有10人回复
【求助】求一个关于文件读写的c语言小程序
已经有3人回复
【求助】VC++.NET编程,MFC基于对话框应用程序编写
已经有18人回复
» 抢金币啦!回帖就可以得到:
中国水产科学研究院黄海水产研究所张旭志课题组招聘 柔性传感器研究 科研助理
+2/156
山东建筑大学 (市政与环境工程学院)硕士研究生招生啦 静电纺丝纤维与盐水分离应用
+1/90
求助有机化学第二版陆阳申东升主编课件或者电子版书籍
+1/85
艾里卡特 (Alicat) - 燃料电池测试系统与质量流量和压力仪表
+3/61
散金币,求基金中标!!
+1/58
青岛大学固态电池研究团队招收材料、化学、物理等方向博士研究生
+2/46
【全额奖学金+英国博士学位】国科大杭高院Nathan课题组招收2026级联培博士生1名!
+1/45
环境化学、地学、环境毒理方向,擅长污染物迁移转化降解、分析检测、计算模拟者优先
+1/35
欢迎08开头或部分07开头的有缘人报考
+1/34
招收2026年材料与化工专硕调剂,山东省属重点大学,过国家线即可调剂,名额充足
+2/30
巴黎西岱大学(Université Paris Cité)Fayna MAMMERI 导师寻找CSC公派博士生
+1/22
中国科学院上海硅酸盐研究所施剑林院士团队胡萍课题组招聘博士后
+1/15
招聘科研助理——储能方向(储能、流体传热、CFD模拟)
+1/12
南京林业大学-国家级青年人才团队 招2026级博士、硕士(合成化学、植物化学方向)
+1/12
2026年材料/冶金/化工硕士调剂-上海大学全国重点实验室-激光冶金与硅基材料制备
+1/11
教育部长江学者和创新团队发展计划”入选团队招收 材料,化学与化工博士研究生
+1/6
武汉纺织大学招收硕士研究生调剂 (社牛最好)
+1/5
【博士招生】天津理工大学国家杰青王铁课题组招收2026年博士研究生
+1/3
双一流高校河南大学量子纳米光子学课题组光学、凝聚态博士招生
+1/2
上海交通大学环境智能感知实验室招聘博士后
+1/1
3楼2011-02-24 12:09:25
2楼2011-02-24 11:47:13
4楼2011-02-24 14:27:42
5楼2011-02-24 14:28:31













;
回复此楼