Opening a .txt file until eof in a prototype

Could someone tell me how to open a file in a prototype? Specifically, I need to open the file and read and store the data until end of file.

The prototype looks like this:

void readData(ifstream& input, int arr[])
That's not a prototype without a semicolon at the end. Also, you wouldn't open it in the prototype, you would do so in the function definition. Or open it before you call the function then pass the object to the function, which is more what it looks like you are trying to do.
Last edited on
Yeah, forgot the semicolon. So, do I just open the file in the main function? And if I have to take some of the data from the file and put it into the array, will I have to do that in the main function or under the prototype's function?
Sure, you can open it in main(). Then you would call readData passing the ifstream object you declare and the array. From the way it sounds you would input from the file to the array inside the function. Otherwise, there's not much point to the function.
Ok, so this is what I have:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void readData(ifstream& input, int arr[]);

int main()
{
	string line;
	ifstream schedule;

	schedule.open ("schedule.txt", ifstream::in);
	if (!schedule)
	{
		return 1;
	}

	while (!schedule.eof())
	{
		getline(schedule, line);
	}
	schedule.close();

	return 0;
}

void readData(ifstream& input, int arr[])
{
}


Now if what I have done is correct so far, how do I pass the file's data to the readData function and call that into main?
By passing the ifstream object to the function. Here the name for the object you declared is schedule. However you haven't declared an array to pass to the function yet, but say you did, and you called it array1 (Ie, you declared it as int array1[20]), your function call would look like:

readData(schedule, array1)

Note that you'll prolly also want to pass the size of the array to the function in a 3rd argument, or you'll want to add code to the function that determines the size of the array itself so it knows when to stop reading into the array and doesn't run out of bounds and write into memory that isn't part of the array. Sure, you'll prolly know for this exercise just how many ints you'll be reading into it, and that's ok to simply hardcode that into the function, but for more complex code, or when you are writing a program where you won't know how many inputs there will be, making your functions know when to read and when not to becomes even more important
I made some changes to it. I hope this is still correct, but i don't really know what exactly the int arr[] in the prototype is supposed to do.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void readData(ifstream& input, int arr[]);

int main()
{
	ifstream schedule;
	schedule.open ("schedule.txt", ifstream::in);

	return 0;
}

void readData(ifstream& input, int arr[])
{
	if (!input)
	{
		exit(1);
	}

	string line;
	while (!input.eof())
	{
		input>>line;
	}
}


I pretty much understand the call now, but I'm still confused on how to declare an array to pass to the function
Assuming you're expected to write the successive integers into the successive positions in the array, you could do it as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

using namespace std;

void readData(ifstream& input, int arr[]);

int main()
{
    ifstream schedule("schedule.txt");
    int arr[1000]; // is 1000 enough?
    readData(schedule, arr);
}

void readData(ifstream& input, int arr[])
{
    while (input >> *arr)
    {
        ++arr;
    }
}

The problem here is that the function readData() doesn't know how many elements did the function main() allocate in the array. If the file has just one more integer than what's allocated, undefined behavior occurs (could be intermittent crash, could be some strange glitches, etc).

What you should do, when reading from a file, is to use a resizeable container:

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;

void readData(ifstream& input, vector<int>& arr);

int main()
{
    ifstream schedule("schedule.txt");
    vector<int> arr;
    readData(schedule, arr);
}

void readData(ifstream& input, vector<int>& arr)
{
    int n;
    while (input >> n)
    {
        arr.push_back(n);
    }
}

(granted, if you were following a better textbook, you'd know that already: vector is the first container normally taught)
Topic archived. No new replies allowed.