ifstream assistance

closed account (4ET0pfjN)
Hi, I have ten values stored in a text like so:16 4 18 9 18 0 15 12 0 13
called randomData10.txt

I want to be able to read from this file and store these data as entries in an array and then sort it. What I have doesn't run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>
using namespace std;
int main()
{
	int arraySize = 10; 
	int *insertionList = new int[10];
	
	//read each item from file and store to each indx in array insertionList
	ifstream myFile;
	int value;
	myFile.open("randomData10.txt");

   for ( int i = 0; i < arraySize; i++)
	{
		myFile>>elem;
		insertionList[i] = elem;
	}

   return 0;
}
Last edited on
Once your file is open, enter a loop that's set to loop 10 times. Then, with each cycle, extract exactly 1 integer from the file. For example:

1
2
for(Integer Cycle; Cycle < 10; ++Cyle)
    Stream >> InsertionListCycle;


Wazzak
Last edited on
closed account (4ET0pfjN)
i used wrong variable, should have been value, :)
No need for value, since you can just extract the value into insertionList directly.

Wazzak
Topic archived. No new replies allowed.