File I/O and Linked List (Best approach?)

File I/O and Linked List (Best approach?)

Post by SienaRo » Wed, 31 Jan 2001 14:03:27



Hi there!  I was wondering if anyone could help me out here.  Here's the
situation:  I have a separate external file that is a list of floats
called "list.txt".  I was wondering what would be the best way to
extract that data and put it into a linked list (each float number per
node).

What would be the best way to do this?  Should I open the "list.txt"
file and count the amount of numbers there are and create an empty
linked list and then add each float into the linked list afterwards?  Or
should I open the file, create the first node and enter the first number
on the list, and loop until no more numbers to create my linked list?
How would I do this?

fstream get_data;

get_data.open("list.txt", ios::in);

while(!get_data.eof())
  {
      // ???????  Any suggstions?
   }

Any help or suggestions is very appreciated!  Thanks!

Derrick

 
 
 

File I/O and Linked List (Best approach?)

Post by Sam Hold » Wed, 31 Jan 2001 14:10:52



>Hi there!  I was wondering if anyone could help me out here.  Here's the
>situation:  I have a separate external file that is a list of floats
>called "list.txt".  I was wondering what would be the best way to
>extract that data and put it into a linked list (each float number per
>node).

>What would be the best way to do this?  Should I open the "list.txt"
>file and count the amount of numbers there are and create an empty
>linked list and then add each float into the linked list afterwards?  Or
>should I open the file, create the first node and enter the first number
>on the list, and loop until no more numbers to create my linked list?
>How would I do this?

Why not?:

ifstream ifs("list.txt");
float data;
list<float> lf;
while (ifs >> data)
        lf.push_back(data);

--
Sam Holden

 
 
 

File I/O and Linked List (Best approach?)

Post by SienaRo » Wed, 31 Jan 2001 14:30:34


By the way, it's in C++.

Derrick


> Hi there!  I was wondering if anyone could help me out here.  Here's the
> situation:  I have a separate external file that is a list of floats
> called "list.txt".  I was wondering what would be the best way to
> extract that data and put it into a linked list (each float number per
> node).

> What would be the best way to do this?  Should I open the "list.txt"
> file and count the amount of numbers there are and create an empty
> linked list and then add each float into the linked list afterwards?  Or
> should I open the file, create the first node and enter the first number
> on the list, and loop until no more numbers to create my linked list?
> How would I do this?

> fstream get_data;

> get_data.open("list.txt", ios::in);

> while(!get_data.eof())
>   {
>       // ???????  Any suggstions?
>    }

> Any help or suggestions is very appreciated!  Thanks!

> Derrick

 
 
 

File I/O and Linked List (Best approach?)

Post by Mike Wahle » Wed, 31 Jan 2001 15:23:08



Quote:> Hi there!  I was wondering if anyone could help me out here.  Here's the
> situation:  I have a separate external file that is a list of floats
> called "list.txt".  I was wondering what would be the best way to
> extract that data and put it into a linked list (each float number per
> node).

[snip]

See my (and the other) replies to this same question you
posted at a.c.l.l.c-c++

-Mike

 
 
 

File I/O and Linked List (Best approach?)

Post by Thomas Matthew » Thu, 01 Feb 2001 09:05:36




Quote:> Hi there!  I was wondering if anyone could help me out here.  Here's the
> situation:  I have a separate external file that is a list of floats
> called "list.txt".  I was wondering what would be the best way to
> extract that data and put it into a linked list (each float number per
> node).

> What would be the best way to do this?  Should I open the "list.txt"
> file and count the amount of numbers there are and create an empty
> linked list and then add each float into the linked list afterwards?  Or
> should I open the file, create the first node and enter the first number
> on the list, and loop until no more numbers to create my linked list?
> How would I do this?

> fstream get_data;

> get_data.open("list.txt", ios::in);

#include <list>
#include <algorithm>
#include <iostream>
#include <iterator>

std::list<float>                my_list;
std::istream_iterator<float>    float_reader(get_data);
std::istream_iterator<float>    float_reader_EOF;

std::copy(float_reader, float_reader_EOF, my_list.begin();

The above code is not tested.

Sent via Deja.com
http://www.deja.com/