473,795 Members | 2,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file read -- increase the size of an array

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.
Nov 13 '05 #1
14 8523

"Sameer" <ia*******@hotm ail.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 ?
Nov 13 '05 #2
"Sameer" <ia*******@hotm ail.com> wrote:

"Sameer" <ia*******@hotm ail.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("inpu t" ,"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?
Nov 13 '05 #3
Irrwahn Grausewitz <ir*****@freene t.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)cybers pace.org |
Nov 13 '05 #4
Christopher Benson-Manica wrote:
Irrwahn Grausewitz <ir*****@freene t.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?


You want to reduce the number of reallocations while not over allocating
memory. Choose a number as an increase amount. When the array is full,
allocate space for another N integers.

Perhaps you want to use a dynamic data structure, such as a linked list.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Nov 13 '05 #5
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
Irrwahn Grausewitz <ir*****@freene t.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?


Depends. Personally, I would choose a third option: realloc() memory
in reasonable big chunks, and when done reading do a last realloc() to
shrink-to-fit (if that is necessary at all, alas the OP's assignment
said so).

Regards,

Irrwahn
--
What does this red button do?
Nov 13 '05 #6
Thomas Matthews <Th************ *************** *@sbcglobal.net > spoke thus:
Perhaps you want to use a dynamic data structure, such as a linked list.


Is that a preferred solution?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cybers pace.org |
Nov 13 '05 #7


Christopher Benson-Manica wrote:
Irrwahn Grausewitz <ir*****@freene t.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?


I would not go through the file twice and I would use function realloc.
Instead of reallocating on every int read from the file, I would
in blocks. For example you can set the reallocations after every five
integers read. Define BLOCK to whatever value is most efficient for your
requirements.

#define BLOCK 5

int *iarray,num;
unsigned count;
FILE *fp;

/* open the file for reading */
for(iarray = NULL, count = 0U;
EOF != fscanf(fp,"%d", &num);count+ +)
{
int *tmp;
if(count%BLOCK == 0U)
if((tmp = realloc(iarray, (sizeof *iarray)*(count +BLOCK)))
== NULL) break;
iarray = tmp;
iarray[count] = num;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.com base.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #8
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
Thomas Matthews <Th************ *************** *@sbcglobal.net > spoke thus:
Perhaps you want to use a dynamic data structure, such as a linked list.


Is that a preferred solution?


Maybe not when dealing with integer items, but of course it is worth
consideration when handling more complex data structures.

Regards

Irrwahn
--
What does this red button do?
Nov 13 '05 #9
Irrwahn Grausewitz wrote:
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
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?

Depends. Personally, I would choose a third option: realloc() memory
in reasonable big chunks, and when done reading do a last realloc() to
shrink-to-fit (if that is necessary at all, alas the OP's assignment
said so).


Is it really that big of a deal to call realloc() every time? Doesn't
realloc() get memory in semi-big blocks anyway? Is it worth the added
complexity to manage the blocks ourselves?

Matt Gregory

Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5469
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. I want to develop a webpage where people can send attachments that are stored on their local PC.
9
35318
by: haibhoang | last post by:
I have a Windows Service that is trying to parse a large (> 1Gig) text file. I am keep getting OutOfMemoryException exception. Here is the code that's having problem: using (StreamReader streamReader = new StreamReader(stream, Encoding.ASCII)) { string line = ""; DateTime currentDate = DateTime.Now.Date; while (streamReader.Peek() > -1)
1
1912
by: Stork via DotNetMonster.com | last post by:
Hi, Just a little background info, I am working on a WinForms program that allows users to have an eletronic version of my catalog, since while they are actually looking at peices they need to buy they won't also have internet access and be able to go to the website. The catalog has a feature to be able to 'save quote' which saves the cart
6
4025
by: elena | last post by:
Hi, All I have text file ASCII with record length of 388 bytes, no record delimeter and size of the file is 562477780 bytes and 1449685 records all togeter. How can i read such file record by record ? Please, help
89
6083
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
9
5212
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
9
2196
by: FFMG | last post by:
In my site I have a config table, (MySQL), with about 30 entries; the data is loaded on every single page load. This is not the only call to the db, (we do a total of about 8 calls to the db). As with many configurations settings, once the options are set the values will not change much. So I thought it would be a good idea to move the data to a flat file. I have written a small wrapper class to first look for the file, if it exists...
19
5387
by: Lee Crabtree | last post by:
Is there a class in the framework that allows me read text from a file in an unbuffered manner? That is, I'd like to be able to read lines in the same manner as StreamReader.ReadLine(), but I also need to be able to accurately get the position in the file of that line. Since StreamReader and FileStream both buffer data, you can't equate the BaseStream.Position with the data you're reading from the stream. Lee Crabtree
4
2124
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.