473,836 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic memory allocation:Read ing a file into buffers using a single linked list!

Hi,

I am new to the c-programming language and at the moment I am
struggling with the following:

I want to read a file using fread() and then put it in to memory. I
want to use a (singel) linked list to continousely (dynamically) free
up more memory when needed and put the buffers with the data of the
file on a sort of stack.
The problem is that i have no idea how to do this.

My question is: Does anyone of you have an example of how to read in an
file and put it into memory using a singel linked list or maybe know a
site where I can find an example?

Thanks in advance.

Greetings Arno

Nov 15 '05 #1
4 3859


Henk wrote On 09/29/05 11:02,:
Hi,

I am new to the c-programming language and at the moment I am
struggling with the following:

I want to read a file using fread() and then put it in to memory. I
want to use a (singel) linked list to continousely (dynamically) free
up more memory when needed and put the buffers with the data of the
file on a sort of stack.
The problem is that i have no idea how to do this.

My question is: Does anyone of you have an example of how to read in an
file and put it into memory using a singel linked list or maybe know a
site where I can find an example?


One way is to use a struct object to hold each
chunk of data, plus a pointer to the struct that
holds the next chunk:

struct chunk {
struct chunk_s *next;
Whatever data;
};

You'll also need a pointer to the first chunk
so you can find the start of the list after you're
done reading, and a pointer to the last chunk read
so far so you know where to add the next one, and
a pointer to the chunk you're currently reading:

struct chunk *head = NULL;
struct chunk *tail;
struct chunk *this;

To read the data, you'll allocate memory for a
chunk and read into its `data' portion. If there's
nothing left to read, discard the unused chunk and
return `head' as a pointer to the start of the list.
Otherwise, append the newly-read chunk to the list
and repeat:

for (;;) {
this = malloc(sizeof *this);
if (this == NULL)
... die ...
... read into `this->data' ...
if (... end of file ...) {
free (this);
return head;
}
if (... I/O error ...)
... die ...
if (head == NULL) /* the first chunk */
head = this;
else /* subsequent chunks */
tail->next = this;
tail = this;
this->next = NULL;
}

There are lots of variations on this approach.
For example, you can do get rid of the `tail' pointer
and the `if (head == NULL)' stuff by building the list
backwards and then reversing it before returning. Or
there might be a `size' field in the `struct chunk' to
state how much of the `data' is actually used. Or ...

--
Er*********@sun .com

Nov 15 '05 #2

Henk wrote:
Hi,

I am new to the c-programming language and at the moment I am
struggling with the following:

I want to read a file using fread() and then put it in to memory. I
want to use a (singel) linked list to continousely (dynamically) free
up more memory when needed and put the buffers with the data of the
file on a sort of stack.
The problem is that i have no idea how to do this.

My question is: Does anyone of you have an example of how to read in an
file and put it into memory using a singel linked list or maybe know a
site where I can find an example?

Thanks in advance.

Greetings Arno


If you want to use dynamic arrays to accomplish the same task, just
grab FreeDOS Edlin, available from ibiblio or alt.sources.

Gregory Pietsch

Nov 15 '05 #3
> I want to read a file using fread() and then put it in to memory. I
want to use a (singel) linked list to continousely (dynamically) free
up more memory when needed and put the buffers with the data of the
file on a sort of stack.
The problem is that i have no idea how to do this.

......I believe, a queue implementation suits well for this case (for
eg. if you want to process file with size larger than main memory, you
have to read it in chunks of constant size and store it in a buffer).
Here the queue can act as a buffer.

Queue can be implemented using a single linked list (maintain both
first and last pointers, always add nodes at Last and remove First Node
--FIFO)
Eg.

struct node
{
char* buffer; //Replace char* with whatever you want
struct node* first;
struct node* last;
};
- Construct a queue with "n" nodes;
- Remove first node (process data and clear buffer)
- Read another chunk of data from file and add it as a last node in
queue.

You can keep loop the last two steps.

My question is: Does anyone of you have an example of how to read in an
file and put it into memory using a singel linked list or maybe know a
site where I can find an example?


.......search for a Queue implementation on google.
- Hemanth

Nov 15 '05 #4
Thanks for all the info guy's it really helped me a lot

Greetings Henk

Nov 15 '05 #5

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

Similar topics

0
1239
by: TNGgroup | last post by:
Hi, I have a dynamic list box with all my records. Whan selecting a value in this box I want to be able to delete the value. but no matter what my selection is, He always deleting the first record. ?? This is my code: <form action="<%=MM_editAction%>" method="POST" name="form_remove" class="content"d="form_remove">
8
7719
by: jmbn2k | last post by:
Hi Can anyone pls teme how to allocate memory using malloc in a 3 D diemensional array.............
13
9734
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not initialized properly as constructor same is not get called ( as per the behavior). How to call a constructor explicitly if we want to allocate memory using malloc ?
11
11777
by: tara99 | last post by:
I was wondering if can some help me with creating a dynamic list box. I have a combobox with the list of items (item1, item2, item3). item1 has 5 sub items item2 has 10 sub items item3 has 2 sub items I want the size of the list box to change depending on the number of sub items available. I hope this is clear.
5
2873
subashini Thiyagarajan
by: subashini Thiyagarajan | last post by:
hi, any one can help me in dynamic list box selection with simple codings. when the first list box item selected depents on the selected item another list box item should change.
8
10434
by: yfguo | last post by:
Do somebody know how to come up with a dynamic file name when ftp it ? I need to download a monthly updated file. The file name appears like datafile_yyyymm.zip where yyyy mean year 2008, and mm mean month 02.
6
2592
by: dasrasmikant | last post by:
Hello, I am new in Ajax. I want to create Ajax dynamic list with ASP and XML. can anybody will help me regarding This. Thanks in advance..
2
1443
by: =?Utf-8?B?SmFzb24gQmFybmV0dA==?= | last post by:
I wrote an aspx page that creates a .pdf file upon request and transfers the file to the client for viewing (via an Adobe Acrobat plug-in). Afterwards, the file is no longer needed and should be deleted from the server. Here is a code snippet as an example: Response.ContentType = "application/pdf"; Response.Clear(); Response.TransmitFile(filename); Response.Flush(); //Response.End();
4
7898
by: pghantasala | last post by:
How to get available physical memory using javascript NOT USING WMI ?? any other way? I need to achieve this by using javascript/vbscript and I cannot use WMI. Please let me know the possibilities.
0
9820
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
10846
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10551
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...
0
9376
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
7793
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
6979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.