Answers for "how to handle files with c++"

C++
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