In: Computer Science
Write a function called printChList that takes as its parameters a character array, its size, and output file stream. The function should print the contents of the array to the output file. code with c++ and detail explaination
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
void printChList(char data[],int s,ofstream *outfile)
{
*outfile << data << endl; //writing text data to the outfile
cout<<"Written into file ...";
}
int main()
{
ofstream outfile; //ofstream is used to write into file
outfile.open("output.txt"); //open() function of ofstream class will open(or create new file if not present) file of specified name in the wrtie mode
int size; //variable declaration required variables
char data[10000];
cout<<"Enter Text to write into the File : ";
cin.getline(data,10000); //getting input of text data
size=strlen(data); //finding size of text data using strlen() function
printChList(data,size,&outfile); //passing char[], size of data and address of outfile object
return 0;
}
OUTPUT
Output.txt
There was a king. He was very clever and humble.
I hope I have solved your problem.
If any query occurs please mention them in the comment section.
Thank you.