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.
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.
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
#include <iostream>
#include <fstream>
usingnamespace 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: