forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofstream.cpp
More file actions
51 lines (42 loc) · 1.29 KB
/
ofstream.cpp
File metadata and controls
51 lines (42 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*******************************************************************************
*
* Program: ofstream Basics
*
* Description: Demonstrates how to use ofstream to write to files in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=Rfe2Jb2JP-Y
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// create the ofstream object
ofstream outFile;
// open file.txt using the append flag so content will be appended to the
// end of any existing content in file.txt
outFile.open("file.txt", ofstream::app);
// check if the file failed to open, fail gracefully if this is the case
if (outFile.fail())
{
cout << "Error opening file." << endl;
return 1;
}
// we use the stream insertion operator to write to the file, as we do
// when we use cout!
outFile << "content" << endl;
// declare and initialize some variables of different types
double x = 4.5;
int y = 10;
string z = "abc";
// we can output the content of variables as well!
outFile << x << endl;
outFile << y << endl;
outFile << z << endl;
// close the file, as a best practice when we are done with it
outFile.close();
return 0;
}