Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ help this is my assignment.............Write a program to open the text file

ID: 3822404 • Letter: C

Question

C++ help this is my assignment.............Write a program to open the text file named "CSC2134.TXT" that you created in assignment 21 for input, then read all the lines of text from the file and display them on the screen.

this is the previous program to write to the file...........

#include <iostream>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string str;
cout<<"Enter a string: ";
std::getline(cin, str);
ofstream myfile;
myfile.open ("CSC2134.txt");
while(str.size()!=0){
myfile << str <<endl;
cout<<"Enter a string: ";
std::getline(cin, str);
}
myfile.close();
return 0;


}

Explanation / Answer

Please let me know in case of any issue.

#include <iostream>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{

   string line;

   // opening fule
   ifstream myfile;
   myfile.open ("CSC2134.txt");

   if(myfile.fail()){

       cout<<"Error in opening file"<<endl;
       return 1;
   }
   while (getline(myfile, line))
   {
   cout<<line<<endl;
   }

   myfile.close();
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote