Hi
I am trying to store an array into a file. I have tried a few ways but I end up with an extra line at the end of the file.
The only way I can do this without the line is by writing
ofstream myfile("Stock.txt");
if (myfile.is_open())
{
myfile << number[0] <<endl;
myfile << number[1] <<endl;
myfile << number[2] <<endl;
myfile << number[3] <<endl;
myfile << number[4] <<endl;
myfile << number[5] <<endl;
myfile << number[6] <<endl;
myfile << number[7] <<endl;
myfile << number[8] <<endl;
myfile << number[9];
}
//if this isnt possible it outputs this
else cout << "Unable to open file";
This does what I want it to do, but is not very efficient as I have to write a new line everytime the data in the array increase.
So i tried it using a for loop like this
ofstream myfile("Stock.txt");
if (myfile.is_open())
{
for(int i=0; i <9; i++)
myfile << number[i] <<endl;
}
but it produces an extra line as I have endl at the end.
I feel pretty sure that I can use a while loop but I have never used a while loop that uses an array and I cannot seem to find this on the net. Could anyone help
Thank you
Liam