Determining array size based on file input

Hi all! I need to write a function that receives an input file and an array, reads the data in the input file, and returns the size of the array, based on how many data points are in the input file. I've never had to do anything like this before; typically I'd set the array's size and then fill it with data. This problem requires me to do it the other way around. If there's any advice you can give, I'd appreciate it. (If this is the wrong forum for this topic, please let me know; I'm new around here.)
Do you need to do anything with the data? What if you just counted it and didn't store it?

If you need to store it, stick it in a vector which increases it's own size as needed.

If you truly must then hold it, in an array, you could even then copy if from the vector into and array once you've got it all.
A vector is the cleanest approach in c++.

Another possibility is to declare the array much larger than you think will be needed, then partially fill it. Return the number of elements used. Check the array boundaries don't get overrun in the case that the input file contains more data than expected. In this case, you might print an error message and end the program.
Thanks to both of you! I don't believe I'm supposed to use vectors; the assignment clearly says to use an array. I think I should use the second approach suggested by Chervil, set the array size large and then reduce it as needed. This leads to a second question (if you don't mind), how would I tell the computer which elements are "used?" In other words, how would I distinguish between an array cell filled with junk and a cell holding useful data? Thanks again for your help!
how would I distinguish between an array cell filled with junk and a cell holding useful data?


You know how many times you read data in. If you start filling from the front, you know how many of the cells hold data.

Basically, count it as you go.
Got it. Thanks!
Topic archived. No new replies allowed.