24小时热门版块排行榜    

查看: 1447  |  回复: 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的回帖

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的回帖
查看全部 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的回帖

bitgreen

金虫 (小有名气)


★ ★
余泽成(金币+2): 谢谢参与应助! 2011-02-27 15:05:56
如果纯粹数据处理,我通常选择shell和mathematica合用,高效省事
3楼2011-02-24 12:09:25
已阅   回复此楼   关注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的回帖
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 欢迎发来filecode的Mz6后的代码验证其规律 +32 医学老男孩 2026-08-13 74/3700 2026-08-16 21:05 by 医学老男孩
[基金申请] 时间戳又变了8-15 +13 archvillain 2026-08-15 25/1250 2026-08-16 20:13 by zhaosm1982
[基金申请] 2027广东省杰青 +3 奶牛小黑 2026-08-15 6/300 2026-08-16 20:07 by 奶牛小黑
[基金申请] 快农历七夕节了,轻松一下,男人悄悄话,女施主请不要进来。 +3 Tide man 2026-08-14 3/150 2026-08-16 17:47 by jurkat.1640
[基金申请] 哪位老哥知道今年的国自然具体哪一天放榜? +9 Ldrop2023 2026-08-13 9/450 2026-08-16 13:24 by wukun_007
[基金申请] 咱们一起用铁证分析2026国家社科基金中标与否 +7 启萌科技 2026-08-12 26/1300 2026-08-16 12:35 by 启萌科技
[基金申请] 我的国基提前知道中了,可是同事的操作让我实在接受不了,怎么会有这样的人 +11 家与远方 2026-08-10 16/800 2026-08-16 10:28 by ray43
[基金申请] filecode=后面第一个是大写字母 +6 wangze12014 2026-08-14 7/350 2026-08-15 20:12 by gltch
[基金申请] filecode +8 cratir 2026-08-14 12/600 2026-08-15 18:08 by zyfgau
[基金申请] 关于Filecode分析方法 +10 majunge000 2026-08-10 13/650 2026-08-15 12:26 by hanpeng972
[基金申请] 各位道友,我要去昆明玩几天,回来见。 +7 Tide man 2026-08-14 8/400 2026-08-15 01:11 by arzu_hma
[基金申请] 是这周出结果还是下周出结果? +4 yuleib84 2026-08-11 4/200 2026-08-14 23:05 by lfy8008
[基金申请] 奇怪,两个人的filecode固定段从头到尾一模一样 +8 布布和一二 2026-08-10 11/550 2026-08-14 14:58 by Equinoxhua
[硕博家园] 读博的好处 +4 lnee 2026-08-11 4/200 2026-08-14 10:20 by ahsoarli
[基金申请] FileCode能看出啥? +10 要乐观耀哥 2026-08-10 32/1600 2026-08-14 09:37 by 要乐观耀哥
[基金申请] 静等基金结果 +8 gjjjzhong 2026-08-10 21/1050 2026-08-13 17:56 by 且听虎啸
[基金申请] 不应该看fileCode +7 且听虎啸 2026-08-12 9/450 2026-08-13 14:27 by flydreamws
[基金申请] 2019年青年基金涵评意见,大家看看几个A,几个B? +11 Tide man 2026-08-11 11/550 2026-08-13 07:35 by 撸猫猫
[基金申请] 帮忙看看fileCode +7 wwncly 2026-08-10 13/650 2026-08-11 19:36 by 冰心玉壶晴
[基金申请] 据悉今年马上要出结果了 +7 瞬息宇宙 2026-08-10 8/400 2026-08-10 12:42 by Vivilian
信息提示
请填处理意见