Thread: Reading all the numbes from a file and storing in an array

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    24

    Reading all the numbes from a file and storing in an array

    is there an easy command to read from a file all the numbers which are stored in the file.

    lets say we have the file arraydata.dat:

    Code:
    3 929 942.3 -2 0 28 92
    
    //and so on.......
    Now, without knowing how many numbers there are in arraydata.dat, is there a way of setting up a while loop until it reads every single number, and store them in an array?

    I can do it so that i can say collect 10 numbers from the file, but not get all the numbers from it (that is when i do not know how many there are).

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I'm not sure how to explain this in technically-perfect-in-every-way terms, so pardon my not-quote-accuracies. (dash, anyone?)

    When you try to extract the end-of-file, the stream will enter an error state, and the ostream& you're using will return false. You can test this value to see when to stop extracting:
    Code:
    int num, i = 0;
    const int MAX_NUM = 10;
    int numArray[MAX_NUM];
    while (cin >> num)
    {
      numArray[i] = num;
      ++i
    }
    cin.clear()
    This will read from cin until an EOF is entered. The cin.clear() returns the cin to a useable state. Otherwise, next time you use cin, it will still be in a fail state. Just adapt this code to your program, replacing cin with your file stream.

    Of course, if there are more than 10 entries in the data file, you will access memory outside of your array, and probably get a segmetation fault. Better to use vectors if you don't know how many there will be.
    Last edited by Decrypt; 04-02-2006 at 02:36 PM. Reason: That's better
    There is a difference between tedious and difficult.

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    And if you're worried about declaring an array too small, maybe you could use a vector? If you need to use an array, just set up a little check to make sure it doesn't read too MUCH data and overflow the array.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  2. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 05:17 AM
  3. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  4. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM
  5. Storing words from a file to an array
    By SIKCAR in forum C Programming
    Replies: 10
    Last Post: 09-09-2002, 06:47 AM