Answers for "c++ how to include files"

C++
17

c++ files

// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
Posted by: Guest on November-09-2019
0

how to handle files with c++

#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;

int main () {
  //write
  ofstream writer("file.txt");
  string test = " /nTest2";
  writer << "Test text";
  writer << test;
  writer.close();
  
  //read
  ifstream reader("file.txt");
  string getter
  while (getline(reader, getter))
			{
				dataAdderInp >> getter;
			}
  
  cout << getter;
  // add a new text to an exist file and keep the text that is in it.
  ofstream add("file.txt", std::ios::out | std::ios::app);
  add << "/n bonus text";
  add.close();
  return 0;
}
//if you run this you will get:
// -a file with: "Text text /n Test2"
// -and a console output: "Text text /n Test2"
// -and in the end your file will get a text, so it will looks like: "Text text /n Test2 /n bonus text"
Posted by: Guest on August-10-2021

Browse Popular Code Answers by Language