Thread: Reading from a file

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    27

    Reading from a file

    Hi! how you all going? I'm hoping you will help me with this little problem

    I was wondering how do I make my program stop reading the file and storing it into the array once it encounters a 'whitespace'. Right now it is reading the whole string.

    --------------
    --------------
    The file that it is reading from is something like this....
    Name 828283
    Name2 4343487
    Name3 45894
    ---------------
    --------------

    #include <iostream.h>
    #include <string.h>
    #include <fstream.h>
    #define size 10

    class List_class
    {
    private:
    char names[size][20];

    public:
    List_class();
    void to_screen ();
    };


    void main(void)
    {
    __List_class num_list;

    __num_list.to_screen ();
    }

    List_class::List_class()
    // reads the list in from a file
    {
    fstream infile ("Names.dat", ios::in);

    for (int a = 0; a < size; a++) {
    __infile >> names[a];
    }

    infile.close();
    }


    void List_class::to_screen ()
    // displays the list to the screen
    {

    cout << "This is the list: " << endl << endl;
    for (int a = 0; a < size; a++) {
    ___cout << names[a] << " ";
    __}
    }


    Greatly appreciated

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    I'm only starting out with C++, I'm still learning the basics as I'm a newbie.


    Is there a simpler way of doing it. It's too advanced what u've got up there.

    Greatly appreciated

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-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12: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, 10:12 PM