Answer to Question #181858 in C++ for virushka

Question #181858

Create separate program file for each mode.

  1. ios:: app
  2. ios::ate
  3. ios::binary
  4. ios::in
  5. ios::nocreate
  6. ios::noreplace
  7. ios::out
  8. ios::trunc
  9. Result file


:



1
Expert's answer
2021-04-16T01:00:56-0400
// writing on a text file using ios::app
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt", ios::app);
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}
// writing on a text file using ios::ate
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt", ios::ate);
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}
// writing on a text file using ios::binary
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt", ios::binary);
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}
// reading a text file using ios::in
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt", ios::in);
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}
// writing on a text file using ios::nocreate
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt", ios::nocreate);
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}
// writing on a text file using ios::noreplace
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt", ios::noreplace);
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}
// writing on a text file usiong ios::out
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt", ios::out);
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}
// writing on a text file using ios::trunc
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt", ios::trunc);
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS