Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Dynamic memory allocation:Reading a file into buffers using a single linked list!

Reply
Thread Tools

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

 
 
Henk
Guest
Posts: n/a
 
      09-29-2005
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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-29-2005


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 ...

--


 
Reply With Quote
 
 
 
 
Gregory Pietsch
Guest
Posts: n/a
 
      09-29-2005

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

 
Reply With Quote
 
Hemanth
Guest
Posts: n/a
 
      09-29-2005
> 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

 
Reply With Quote
 
Henk
Guest
Posts: n/a
 
      10-04-2005
Thanks for all the info guy's it really helped me a lot

Greetings Henk

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
resolve single line with multiple items into mutliple lines, single items ela Perl Misc 12 04-06-2009 06:47 PM
Do buffers always start with the lowest memory address being the first element? kiru.sengal@gmail.com C Programming 6 01-19-2005 06:25 AM
WriteFile buffers in memory Tim Greenfield ASP .Net 5 10-05-2004 04:39 PM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57