reading from a file

This is a discussion on reading from a file within the C++ Programming forums, part of the General Programming Boards category; i have a list of names in a file but i dont know how many like bob joe john and ...

  1. #1
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    reading from a file

    i have a list of names in a file but i dont know how many like
    bob
    joe
    john
    and i need them in different variables so my first idea was to use getline to put them into an array of pointers. but it just made the variables full of junk characters. any ideas on how i could do this
    +++
    ++
    + Sekti
    ++
    +++

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    STL vector or list class templates?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    You could make a linked list that will hold the names, and then infile the names in. Every new name gets its own node. For example:

    Code:
    char * name;
    ifstream names("name.nms");
    while (name!=NULL) // Keep bringing in names until file is ended
    {
         name >> name;
         node * next = new node(name);
    }
    That should work, though that loop part may not you may have to work on that. Hope that helps.

    Brendan

  4. #4
    Unregistered
    Guest

  5. #5
    Unregistered
    Guest
    struct node
    {
    char name[80];
    node *next;
    }

    class list
    {
    //code
    public:
    void addNode(node *);
    //code
    };

    int main()
    {
    ifstream fin("myfile.txt")
    list myList;
    while(!fin.eof())
    {
    node * newNode = new node;
    fin.getline(newNode->name, 79);
    newNode->next = NULL;
    myList.addNode(newNode);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21