Reading from a file to an array?

Here's what I have. I just need to know how to put the numbers from infilearray.txt into the array and how to output it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #include <iostream>
#include <fstream>

using namespace std;

void readFromFiletoArray(ifstream &infile, int theArray[], int &arrayCount);

const int SIZE = 30;

int main()
{

	return 0;
}

void readFromFiletoArray(ifstream &infile, int theArray[], int &arrayCount)
{
	
	infile.open("infilearray.txt");
	if (infile.fail())
	{
		cout << "Error: Cannot open input file. Exiting...";
		system("pause");
	}

	theArray[SIZE];
	while (!infile.eof())
	{ }

}

void printArray(ifstream &fileinput)
{
	readFromFiletoArray(fileinput);
	cout << fileinput << endl;
}


And here's what's in infilearray.txt:

 
48 59 58 38 28 38 27 63 93 58 20 58 29 
After you declare an ifstream object treat it like cin.

Use a for loop to loop through and set it to the index of the array.

Example:
for(int i ...){
infile >> array [i] ; }

To output it just loop through the array and cout the numbers as you see fit.
Thanks, I have this now, only error being that the printArray identifier wasn't found in main. How can I fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void readFromFiletoArray(ifstream &infile, int theArray[], int &arrayCount);

const int SIZE = 30;

int main(ifstream &infile, int anArray[], int &numCount)
{

	printArray(infile, anArray, numCount);
	return 0;
}

void readFromFiletoArray(ifstream &infile, int theArray[], int &arrayCount)
{
	arrayCount = 0;

	infile.open("infilearray.txt");
	if (infile.fail())
	{
		cout << "Error: Cannot open input file. Exiting...";
		system("pause");
	}

	theArray[SIZE];
	for (int i = 0; i < arrayCount; i++)
	{
		infile >> theArray[i];
	}

}

void printArray(ifstream &fileinput, int array[], int &count)
{
	readFromFiletoArray(fileinput, array, count);
	cout << array;
}
Use a forward declaration like you did with readFromFiletoArray.

I'm sorry, what do you mean?
a forward declaration is like:

1
2
3
4
5
6
7
8
9
10
11
int someFun(std::string);

int main()
{
  return someFun("foo");
}

int someFun(std::string s)
{
  return s.size();
}


it allows you to use someFun in main, even though the function is defined afterwards.

Line 7 is a declaration and corresponds to the definition on line 18. You have another definition on line 37. You can either move the definition to an earlier point in the file, or you can create a corresponding declaration at an earlier point in the file.
Last edited on
So like this:

1
2
3
4
5
6
7
8
9
10
11
12
void readFromFiletoArray(ifstream &infile, int theArray[], int &arrayCount);

void printArray(ifstream &fileinput, int array[], int &count);

const int SIZE = 30;

int main(ifstream &infile, int anArray[], int &numCount)
{

	printArray(infile, anArray, numCount);
	return 0;
}


But, and I've never seen this before, whenever I try to run it, the cmd pops up, but the windows diagnostic screen that says "title.exe has stopped working." also pops up.
Your program is crashing. Try using your debugger to debug it.
What's supposed to happen when I start debugging it?
Well generally, if you start debugging from the crash, you can see the values of all the variables to figure out why it crashed.

If you set a breakpoint a few lines before the crash you can step through the code one line at a time and figure out which lines are doing what changes.
Topic archived. No new replies allowed.