I am passing a text file of integers to a function by reference. They are separated by a new line. I am wanting to push them into a new array in the function. I can count the number of lines no problem, and I use that variable to create the array of that given size, so a vector is not necessary. But when I print what should be the contents of my array it is way off. Do I need to convert the integers from the file into int values again (since I am reading from a text file, is it reading them as strings?) Help?
C++ Code:
void myFunction(ifstream& infile)
{
int NumberOfLines = 0;
int index;
string line;
//Count the number of lines in the file
if(infile.is_open())
{
while(getline(infile, line))
{
++NumberOfLines;
}
}
int arr[NumberOfLines];
//Put the numbers into an array
while(!infile.eof())
{
infile>>arr[NumberOfLines];
}
//Print the array
for(int i=0; i<NumberOfLines; i++)
{
cout<<arr[i]<<endl;
}
}