Hi, I have to write a function in C++ that reads a single line of text (includin
ID: 3555231 • Letter: H
Question
Hi,
I have to write a function in C++ that reads a single line of text (including the first newline encountered) from the input stream to the output stream.
The input is:
inFile [IN/OUT] -- a reference to the input file stream
outFile [IN/OUT] -- a reference to the output file stream
And the output is:
A value of true if EOF has been encountered, false otherwise.
The prototype would look like this:
bool FunctionWrite(ifstream &inputFile, ofstream &outputFile);
By the way, you can assume that the files are already open and ready to write.
Explanation / Answer
#include<fstream.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
char c,u;
char fname[10];
clrscr();
ofstream out;
cout<<"Enter File Name:";
cin>>fname;
out.open(fname);
cout<<"Enter the text(Enter # at end) "; //write contents to file
while((c=getchar())!='#')
{
u=c-32;
out<<u;
}
out.close();
ifstream in(fname); //read the contents of file
cout<<" The File contains ";
while(in.eof()==0)
{
in.get(c);
cout<<c;
}
getch();
}
Output:
Enter File Name: two.txt
Enter contents to store in file (enter # at end)
oops programming
The File Contains
OOPS PROGRAMMING
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.