Thread: reading from file of unspecified size into array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Unless you've learned some sort of dynamic array (using vector or new[]/delete[]) my guess is that you're supposed to just come up with some maximum size for the array. So assume that the file will have no more than 1000 entries or something like that.

    The thing you'd have to change with your code is identifying how to stop inputting. There is actually a simple technique for that.
    Code:
    while (infile >> array[i])
    That will loop while there are numbers in the file. You'll have to adjust the code to count the number of items properly, but that should be a good starting point. Another option is to use the same technique in an if statement combined with your current for loop so you can break out of the loop if the read fails:
    Code:
    if (!(infile >> array[i]))
    Last edited by Daved; 08-20-2009 at 07:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a txt file into an array: unexpected segfault
    By pistacchio in forum C Programming
    Replies: 3
    Last Post: 05-05-2009, 04:27 PM
  2. Reading from a file into an array
    By fmsguy06 in forum C Programming
    Replies: 6
    Last Post: 10-18-2008, 09:25 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  5. reading a file into an array
    By dantegl36 in forum C Programming
    Replies: 11
    Last Post: 12-02-2006, 12:23 AM