24小时热门版块排行榜    

查看: 1481  |  回复: 5
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

nenyan

铜虫 (初入文坛)


[交流] 【求助】菜鸟求助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 fprintf(out, "%d, %s, %e, %e, %e, %e \n", pgc, name+i*100, rag, decg, absmag, dist);


free(pgc);
free(name);
free(rag);
free(decg);
free(absmag);
free(dist);

fclose(pFile);
fclose(out);
}
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

bitgreen

金虫 (小有名气)


★ ★
余泽成(金币+2): 谢谢参与应助! 2011-02-27 15:05:56
如果纯粹数据处理,我通常选择shell和mathematica合用,高效省事
3楼2011-02-24 12:09:25
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 6 个回答
★ ★ ★ ★ ★
余泽成(金币+5): 谢谢参与应助! 2011-02-27 15:05:48
(1)建议您认真看看scanf函数的读取格式(%后面那几个字母)的含义。
如%*s,%*d等。应该很简洁,您写得太......繁琐了
(2)指针的分配、释放,应该在同一个函数里。
(3)永远不要假设要读取的文件满足您想象的格式
(4)feof()不能这么用。您能找到的所有feof例子(包括教科书、教程),几乎都是错误的。
2楼2011-02-24 11:47:13
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

nenyan

铜虫 (初入文坛)


引用回帖:
Originally posted by yalefield at 2011-02-24 11:47:13:
(1)建议您认真看看scanf函数的读取格式(%后面那几个字母)的含义。
如%*s,%*d等。应该很简洁,您写得太......繁琐了
(2)指针的分配、释放,应该在同一个函数里。
(3)永远不要假设要读取的文件满足您想 ...

是%*s吗,哪里可以查到呢。给一个建议吧。

指针在子函数中就释放吗?后面主函数中直接当成数组用就可以了吗?

文件是被我处理成那个格式的。。。
4楼2011-02-24 14:27:42
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

nenyan

铜虫 (初入文坛)


引用回帖:
Originally posted by bitgreen at 2011-02-24 12:09:25:
如果纯粹数据处理,我通常选择shell和mathematica合用,高效省事

呃,mathematica似乎读入数据也很麻烦。
5楼2011-02-24 14:28:31
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[公派出国] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +6 s3fFTmArrBt6 2026-09-14 6/300 2026-09-17 02:38 by Tql5LQhh5rLK
[硕博家园] 售SCI一区文章,我:8O5.5.1.O5.4,科目全,可伽急 +6 s3fFTmArrBt6 2026-09-13 8/400 2026-09-17 02:04 by Tql5LQhh5rLK
[考研] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +7 QUjhNVAcOSff 2026-09-13 8/400 2026-09-17 01:16 by Tql5LQhh5rLK
[考博] 售SCI一区T0P文章,我:8.O.55.1.O.5.4,科目全,可+急 +3 vZfe6xYu34yj 2026-09-14 3/150 2026-09-16 22:52 by Tql5LQhh5rLK
[博后之家] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +4 vZfe6xYu34yj 2026-09-14 4/200 2026-09-16 22:40 by Tql5LQhh5rLK
[考研] 售SCI一区文章,我:8O5.5.1.O5.4,科目全,可伽急 +3 23jxep3nCNZb 2026-09-14 3/150 2026-09-16 20:50 by MHf40yA0Uhyb
[博后之家] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +5 s3fFTmArrBt6 2026-09-14 7/350 2026-09-16 19:27 by rRvxEj3zorCy
[考博] 上海工程技术大学激光智能制造课题组|2027级博士研究生招生公告 +7 水士口 2026-09-16 8/400 2026-09-16 13:47 by kkklens
[博后之家] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 vZfe6xYu34yj 2026-09-14 3/150 2026-09-16 08:51 by SjQobnC04j06
[找工作] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +3 Aj1rhIDL5ixY 2026-09-14 3/150 2026-09-16 07:12 by LH5NkK5Ud4bh
[硕博家园] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +3 0pYnUiPDfdnk 2026-09-14 3/150 2026-09-16 06:51 by LH5NkK5Ud4bh
[找工作] 售SCI一区T0P文章,我:8.O.55.1.O.5.4,科目全,可+急 +3 40ms4Wlo4umh 2026-09-14 3/150 2026-09-16 06:48 by LH5NkK5Ud4bh
[找工作] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +3 6F5UbRU2I5hL 2026-09-14 3/150 2026-09-16 05:15 by LH5NkK5Ud4bh
[公派出国] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +3 Aj1rhIDL5ixY 2026-09-14 3/150 2026-09-15 19:59 by CgyNCDVNhGVg
[公派出国] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +4 3n8v2C8RimXI 2026-09-13 5/250 2026-09-15 14:58 by CgyNCDVNhGVg
[公派出国] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +3 6F5UbRU2I5hL 2026-09-14 3/150 2026-09-15 08:58 by C79jjtjAKjEn
[博后之家] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +6 3n8v2C8RimXI 2026-09-13 6/300 2026-09-15 05:50 by 5BDX0d0WFp7t
[教师之家] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +6 3n8v2C8RimXI 2026-09-13 6/300 2026-09-15 05:38 by 5BDX0d0WFp7t
[教师之家] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +3 LwdutQ8HoqWP 2026-09-13 4/200 2026-09-14 16:13 by Aj1rhIDL5ixY
[基金申请] 求教各位大神:2026教育部人文社科青年基金项目何时公示呀? +5 云宴山人 2026-09-10 7/350 2026-09-14 12:11 by ecnu2013
信息提示
请填处理意见