-
Notifications
You must be signed in to change notification settings - Fork 0
/
appendExample.cpp
39 lines (30 loc) · 1 KB
/
appendExample.cpp
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
// appendExample.cpp
// This program demonstrates how to append a file.
#include<fstream>
#include<iostream>
using namespace std;
int main()
{ //declare variables
string first, last;
float grade = 0.0;
//declare a file pointer
ofstream outfile; //ofstream is a file pointer used for output to a file
//intro
cout << "WELCOME TO THE STUDENT DATA COLLECTOR!\n\n";
//prompt for info
cout << "Enter first name: ";
cin >> first;
cout << "Enter last name: ";
cin >> last;
cout << "Enter grade: ";
cin >> grade;
//open file "grades.txt" for appending
outfile.open("grades.txt", ios::app); //app will point to the end of the file
//if the file does not exist, it will get created the first time
//if the file already exists, it will be appended
//write to file
outfile << first << " " << last << " " << grade << endl;
//close file
outfile.close();
return 0;
}