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

Create a new file “inFile.txt” and write some words to it Write a program that r

ID: 3820814 • Letter: C

Question

Create a new file “inFile.txt” and write some words to it

Write a program that reads from a file (inFile.txt) and writes everything to a second file (outFile.txt)

Recall: getline(cin, string)

This is what I have so far:

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

int main() {
   ifstream inStream;
   ofstream outStream;

   inStream.open("inFile.txt");
   outStream.open("outFile.txt");

   int fileNum1 = 0;
   int fileNum2 = 0;

   inStream >> fileNum1;
   outStream << fileNum2 << endl;

      inStream.close();
      outStream.close();
    
      return 0;
}

Explanation / Answer

#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inStream;
ofstream outStream;

inStream.open("inFile.txt");
outStream.open("outFile.txt");

int fileNum1 = 0;
int fileNum2 = 0;

string line;
while (getline(inStream, line))
{
outStream << line<<endl;
}
cout<<"File has been copied"<<endl;

inStream.close();
outStream.close();
  
return 0;
}

Output:

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                                     

sh-4.2$ main                                                                                                                                                                                                                                                             

File has been copied

inFile.txt

dsfsdfsd
fadsfasdfadsfadsf
adsf
ads
fas23423
4
23
423
423rweafwadsfasd
fadsfasdf

outFile.txt

dsfsdfsd
fadsfasdfadsfadsf
adsf
ads
fas23423
4
23
423
423rweafwadsfasd
fadsfasdf

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