Read integers from a text file and store into array

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! :)
The idea is to use that value as the reading condition
1
2
3
4
5
6
infile >> amount;

myArray = new int[amount]; // If you know about "new" 

for (int i = 0; i < amount; i++)
  infile >> myArray[i];


This type of file organization is common, especially in binary files.
Last edited on
So, if I'm reading this right:

1) infile is reading each individual integer in the text file. So, I store the first integer in "amount," through the use of infile.

2) Use the new variable "amount" to initialize the array. Is "amount" an int variable, I guess?

3) Use a for loop to populate the array based on the rest of the values in the text file, again by using infile.

Is that about right?

Thank you very much for the code example!
#1 and #3 are right for sure. #2 depends on how much you have learned already. If this is for class, and you haven't seen new before, then don't use it, just do something like:
1
2
3
4
5
6
int myArray[100]
int size;

//Read size
//loop through file size amount of times and read into myArray
//output/use data 



If you have seen new, then use that:
1
2
3
4
5
6
7
8
int *myArray;
int size;

// Read size
// allocate space for myArray
// Loop size amount of times reading into myArray
// output/use your data
// delete myArray 

Last edited on
I was thinking my start needed to be something like this:
while (!file.eof())

Make sure to abandon whatever tutorial or book you got that line from, it's an error. The input loop should terminate if either the input operation fails or the required number of integers were read.

Also, the appropriate data structure in this case is a vector, not an array.

For example, you could write

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
    ifstream file("textfile.txt");
    int size;
    file >> size;
    vector<int> a(size);

    int n = 0;
    while(n < size && file >> a[n])
    {
        ++n;
    }

    for(int n = 0; n < size; ++n)
    {
        cout << a[n] << ' ';
    }
    cout << '\n';
}

Ahh, nope. I haven't messed with new yet. I'll study it after I finish the original program design.

This isn't for a class, but it is a practice-type assignment. I may start classes in the fall, so I'm trying to get as much practice as I can beforehand.

Thanks for all your help, LowestOne! :)

EDIT: Sorry, Cubbi. Didn't see your post before I had replied. Thanks for the suggestion on file.eof. Will avoid it!

The original practice assignment I'm working off of seems to suggest an array is possible. Maybe I should just go with vectors as the better choice. Thanks for the code example, as well!
Last edited on
Topic archived. No new replies allowed.