loop to read values from a file

Apr 5, 2011 at 5:42pm
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?
Apr 5, 2011 at 5:46pm
It could be for(int i = 0; i < array_len && file >> array[i]; i++); or something more readable.
Apr 5, 2011 at 5:48pm
haven't learned anything about array_len yet. is there an alternative to using it?
Apr 5, 2011 at 5:53pm
array_len = short hand for Array Length, it's the name of the relavent variable.
Apr 5, 2011 at 6:06pm
I would rather use a pointer
Apr 5, 2011 at 6:09pm
Then replace array[i] by *(array+i)...
Apr 5, 2011 at 6:13pm
how does array_len get declared?
Apr 5, 2011 at 6:13pm
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
Apr 5, 2011 at 6:20pm
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 Apr 5, 2011 at 6:22pm
Topic archived. No new replies allowed.