Skip to content

Instantly share code, notes, and snippets.

@victorfei
Created September 30, 2016 08:20
Show Gist options
  • Select an option

  • Save victorfei/ff7d2568b73c36f6eb50fcd9ce1d64ef to your computer and use it in GitHub Desktop.

Select an option

Save victorfei/ff7d2568b73c36f6eb50fcd9ce1d64ef to your computer and use it in GitHub Desktop.
A simple C++ snippet for writing to file on disk.
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ofstream msgFile;
msgFile.open("msg", ios::out);
// exit program if ifstream could not open file
if (!msgFile)
{
cerr << "File could not be opened" << endl;
exit(EXIT_FAILURE);
}
char msg[11] = {'h', 'e', 'l', 'l', 'o', ' ', 'y', 'o', 'u', '\0'};
msgFile.write(msg, 11);
msgFile.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment