Hello, everybody! :)
I'm trying to write a program that reads integers from a text file, and then stores the values into an array. The trick is I'd like to have the first integer be stored as the number of "switchFlips" that will take place. So, for example, let's say I have a text file like so:
5 1 0 0 1 1
...or it could be like this:
7 1 0 0 1 1 0 1
As you can see, the first number will give the size of the array, and the rest of the numbers will mark which switches are on and which switches are off.
I've seen a few other topics (and of course the file reading tutorial) on reading from a text file, but I haven't found anything on capturing the first number for an array size, before storing the following values into an array.
I'm sure there are more efficient ways to do this, but I'd really like to keep it storing the first value into a variable, then storing the rest into an array.
I was thinking my start needed to be something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file("textfile.txt");
int switchFlips = 0;
while (!file.eof())
{
//SOME CODE TO READ AND STORE FIRST INTEGER INTO SWITCHFLIPS
file >> tempArray[switchFlips];
}
|
Again, this is just a rough pseudo-code idea I'm playing with above. Am I on the right track?
Thanks all! :)