Answers for "cpp read and write to file"

C++
17

c++ writing to file

// 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
1

write in file cpp

#include <iostream>
#include <fstream>
using namespace std;
int main() {
	fstream my_file;
	my_file.open("my_file.txt", ios::out);
	if (!my_file) {
		cout << "File not created!";
	}
	else {
		cout << "File created successfully!";
		my_file << "Guru99";
		my_file.close();
	}
	return 0;
}
Posted by: Guest on February-02-2021
0

write to file in c++

++ cCopy#include <iostream>
#include <fstream>

using std::cout; using std::ofstream;
using std::endl; using std::string;
using std::fstream;

int main()
{
    string filename("tmp.txt");
    fstream file_out;

    file_out.open(filename, std::ios_base::out);
    if (!file_out.is_open()) {
        cout << "failed to open " << filename << '\n';
    } else {
        file_out << "Some random text to write." << endl;
        cout << "Done Writing!" << endl;
    }

    return EXIT_SUCCESS;
}
Posted by: Guest on September-24-2021

Browse Popular Code Answers by Language