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

There's a couple ways to avoid storing that extra '\n' at the end of the text file. One way is like this:

ofstream myfile("Stock.txt");
if (myfile.is_open())
{
   for(int i=0; i <8; i++)				 
      myfile << number[i] <<endl;
   myfile << number[i];
}

Edited 5 Years Ago by Ancient Dragon: n/a

The reason I cannot use the extra line code, is because I need to output the array when the user asks. When I output the array and there is an extra line, it shows the first line again. Say I had an array with {20, 30, 30}. WHen I call that array to be output, it shows it like this {20, 30, 30, 20}. I assume it is the extra line due to it only outputs the extra 20 if I use endl during the forloop.
I will try the <8 thanks and get back to you with my results.
Thank you very much for your help :)

There's a couple ways to avoid storing that extra '\n' at the end of the text file. One way is like this:

ofstream myfile("Stock.txt");
if (myfile.is_open())
{
   for(int i=0; i <8; i++)				 
      myfile << number[i] <<endl;
   myfile << number[i];
}

Hi Ancient Dragon

Thanks for the help. I tried that way but it missed the last two values that should have been stored in the array. Could you tell me the otherway you were going to mention. Although my method works by inputting one line of the array at the time, it just seems a bit messy.

Thanks in advance

Liam

Post what you tried because you must have made a mistake.

This is the coding I used

ofstream myfile("Stock.txt");
  if (myfile.is_open())
    {
							 
	for(int i=0; i <10; i++)
	 {
	   myfile << number[i] <<endl;
	   myfile << number[i];
	 }  
    }

I also tried using <8 but my array is at present 10.

The output I get when using 8 missed off some of the stored data but also added to the end of the line.

So instead of outputting
17
18
19
18
etc.

it did this
1717
1818
1919
1818

It also does this for the 10 too.
Liam

Edited 5 Years Ago by llamaboy: n/a

Yes you are right, my code was wrong, I used to many braces and it does work the way you said. Thank you very much. The reason I changed my code was because it told me I had an undeclared variable i, so I changed it to include the braces and the effect was the
1717
1818
1717.

I moved the int i to before the loop and it worked. Thank you.

Could you explain how it works? I cannot seem to figure it out. I assume its something to do with number being over written with number endl then number overwriting it again at the end to get rid of the endl on the last loop?

There's nothing magic about what I posted. Just a simple loop that adds endl to the end of all but the last line. Instead of an loop of 10, and it a loop of 9. After the loop finishes the last line is saved without endl.

Edited 5 Years Ago by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.