loop to read values from a file

can someone please show me what a loop to read each value from a .txt file into the elements of an array would look like?
It could be for(int i = 0; i < array_len && file >> array[i]; i++); or something more readable.
haven't learned anything about array_len yet. is there an alternative to using it?
array_len = short hand for Array Length, it's the name of the relavent variable.
I would rather use a pointer
Then replace array[i] by *(array+i)...
how does array_len get declared?
you must know what type of data are written in that file and depending of what do you prefer you can use
http://www.cplusplus.com/reference/clibrary/cstdio/
or
http://www.cplusplus.com/reference/iostream/fstream/
libraries
horance89:
you must know what type of data are written in that file

No, not really. You only need to know what type of data you want to work with from that file. If the number 7 is read into the stream I could make that a char, string, int, float, double, etc. ad infinum. It's read in as a void pointer so it can turn into just about anything you want it to be.EDIT: This seems like I'm being padantic but really, it's something worth noting to a new user.

@OP: You declare "array_len" as what ever data type you want it to be. It is a place holder since we don't know what you code looks like. That's how we demonstrate things like that around here.
Last edited on
Topic archived. No new replies allowed.