文件读取 - 增加数组的大小 [英] file read -- increase the size of an array

查看:66
本文介绍了文件读取 - 增加数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


i希望读取一个int文件并动态存储到一个数组中......

最后分配的内存大小,应该只是足够存储n

整数。

我不知道文件中的整数数量...

我该怎么办?动态创建数组int array [n]?

文件是


1

2
3

4

5

...

....
....

....


等等n $

请尽快回复,


Sameer。

解决方案



" Sameer" < IA ******* @ hotmail.com>在消息中写道

news:bk ************ @ ID-160782.news.uni-berlin.de ...

你好,
我希望读取一个int文件并动态存储到一个数组中......
最终分配的内存大小,应该足以存储
n整数。 br />我不知道文件中整数的数量......
我该如何动态创建数组int array [n]?

文件是

1
2
3
5
...
...
...
...

等等n $

请尽快回复,

Sameer。



# include< stdio.h>

main()

{

int i = 0,j = 0,k,num,n;

int * a;

FILE * fp;

fp = fopen(" input"," r");


while(fscanf(fp,"%d",& num)!= EOF)

{

i ++;

}


a = new int [i];


for(k = 0; k< i; k ++)

{

a [k] = 0;

}

fclose(fp);

fp = fopen(输入) ,r,;

while(fscanf(fp,"%d",& num)!= EOF)

{

a [j] = num;

j ++;

}


for(k = 0; k< i; k ++ )

{

printf("%d \ n",a [k]);

}

//免费[n];


fclose(fp);

返回0;

}

这是一种方式......但我正在阅读文件两次......

只读一次文件......是否可能?


"萨米尔" < IA ******* @ hotmail.com>写道:


Sameer < IA ******* @ hotmail.com>在消息中写道
新闻:bk ************ @ ID-160782.news.uni-berlin.de ...

你好,
我希望读取一个int文件并动态存储到一个数组中......
最后分配的内存大小应该足以存储

整数。
我不知道文件中整数的数量......
我该如何动态创建数组int array [n]?



< SNIP> #include< stdio.h>

main()
{
int i = 0,j = 0,k,num,n;
int * a;
FILE * fp;
fp = fopen(" input"," r");

while(fscanf(fp,"%) d",& num)!= EOF)
{
i ++;
}
a = new int [i];


^^^


这不是C程序。


我的建议:使用malloc()或realloc()随时随地动态增长你的
''数组''。这样你只需要通过你的文件

一次。


< SNIP>


问候


Irrwahn

-

这个红色按钮有什么作用?


< blockquote> Irrwahn Grausewitz< ir ***** @ freenet.de>这样说:

我的建议:使用malloc()或realloc()来动态增长你的数组''。这样你只需要浏览一次文件。




我的问题是,为每个整数调用realloc()是否更有效,或者

两次通过文件并只调用malloc()一次?


-

Christopher Benson-Manica | Jumonji giri,荣誉。

ataru(at)cyberspace.org |


Hello,

i wish to read a file of int and store into an array dynamically...
the size of memory allocated finally, should just be sufficeient to store n
integers.
I do not know the number of integers in the file...
How should I go about creating the array int array[n] dynamically ?

the file is

1
2
3
4
5
...
....
....
....

so on upto n
Kindly reply soon,

Sameer.

解决方案


"Sameer" <ia*******@hotmail.com> wrote in message
news:bk************@ID-160782.news.uni-berlin.de...

Hello,

i wish to read a file of int and store into an array dynamically...
the size of memory allocated finally, should just be sufficeient to store n integers.
I do not know the number of integers in the file...
How should I go about creating the array int array[n] dynamically ?

the file is

1
2
3
4
5
..
...
...
...

so on upto n
Kindly reply soon,

Sameer.


#include<stdio.h>
main()
{
int i=0,j=0,k,num,n;
int* a;
FILE *fp;
fp=fopen("input" ,"r");

while(fscanf(fp,"%d", &num)!=EOF)
{
i++;
}

a=new int[i];

for(k=0;k<i;k++)
{
a[k]=0;
}
fclose(fp);
fp=fopen("input" ,"r");
while(fscanf(fp,"%d", &num)!=EOF)
{
a[j]=num;
j++;
}

for(k=0;k<i;k++)
{
printf("%d\n", a[k]);
}
//free a[n];

fclose(fp);
return 0;
}
Here is one way ...but I am reading the files twice ...
By reading the file only once ...is it possible ?


"Sameer" <ia*******@hotmail.com> wrote:


"Sameer" <ia*******@hotmail.com> wrote in message
news:bk************@ID-160782.news.uni-berlin.de...

Hello,

i wish to read a file of int and store into an array dynamically...
the size of memory allocated finally, should just be sufficeient to storen

integers.
I do not know the number of integers in the file...
How should I go about creating the array int array[n] dynamically ?


<SNIP>#include<stdio.h>
main()
{
int i=0,j=0,k,num,n;
int* a;
FILE *fp;
fp=fopen("input" ,"r");

while(fscanf(fp,"%d", &num)!=EOF)
{
i++;
}

a=new int[i];


^^^

This is not a C program.

My suggestion: use malloc() or realloc() to dynamically grow your
''array'' as you go. This way you have to go through your file only
once.

<SNIP>

Regards

Irrwahn
--
What does this red button do?


Irrwahn Grausewitz <ir*****@freenet.de> spoke thus:

My suggestion: use malloc() or realloc() to dynamically grow your
''array'' as you go. This way you have to go through your file only
once.



My question is, is it more efficient to call realloc() for every integer, or
to go through the file twice and call malloc() only once?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |


这篇关于文件读取 - 增加数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆