0

I want to write a program that will copy a few lines in a file in another file. for example copy line 5 to 100 from file a and write it to file b. thanks

this is my code.but my code copying all content of file a to file b.

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

int main(){
   char line[100];

   ifstream is("a.txt");
   ofstream os("b.txt");

   if (is.is_open()){
      while (!is.eof()){
         is.getline(line,100,'\n');
         os << line << endl;
         }
   }
   else{
      cout << "a.txt couldn't be opened. Creat and write something in a.txt, and try again." << endl;
   }

   return 0;
}
kebs
  • 6,387
  • 4
  • 41
  • 70
alibag90
  • 17
  • 4
  • 8

1 Answers1

2

Here is another way to do it:

 std::ofstream outFile("/path/to/outfile.txt");
 std::string line;

 std::ifstream inFile("/path/to/infile.txt");
 int count = 0;
 while(getline(inFile, line)){

     if(count > 5 && count < 100){
        outFile << line << std::endl;
     }
     count++;
 }
 outFile.close();
 inFile.close();
Jay
  • 2,656
  • 1
  • 16
  • 24