C++ Save Array Data into Text File
Posted by Samath
Last Updated: January 05, 2017

This program writes array data in a text file using C++. It uses a character array but it can be changed to any other data type.

#include <iostream> 
#include <fstream>  
using namespace std;

int main()
{
  
	char my_array[] = {'P','r','o','g','r','a','m','\0'}; 
  
	ofstream fout("Myfile.txt");
  if(fout.is_open())
	{

		for(int i = 0; my_array[i] != '\0'; i++)
		{
      fout << my_array[i]; 
		}
    cout << "Success!" << endl;
	}
	else 
	{
		cout << "File could not be opened." << endl;
	}
	return 0;
}