Created
January 13, 2022 01:06
-
-
Save GermanHoyos/a57574c7d1cb0c9182e9f25555713ff9 to your computer and use it in GitHub Desktop.
Working with File IO in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//File I/O in 5 steps | |
#include <iostream> | |
#include <string> | |
#include <fstream> //Step 1)Include the header file fstream in the program. | |
#include <iomanip> | |
using namespace std; | |
int main() { | |
string firstName, lastName; | |
double curSal, payInc, newSal; | |
//Step 2)Declare file stream variables | |
ifstream inData; | |
ofstream outData; | |
//getLine needs a string as its second param. User inputs file name | |
string inFlString; | |
cout << "Please enter file name, including file extension: "; | |
getline(cin, inFlString); //user puts in file with extension. "Ch3_Ex5Data.txt" | |
//Step 3)Associate the file stream with I/O sources. Program writes to this file | |
inData.open(inFlString); | |
outData.open("Ch3_Ex5Output.dat"); | |
//Step 4)Use the file stream variable with >>,<<, or other I/O functions. Code for data manipulation | |
while (inData >> lastName >> firstName >> curSal >> payInc) { | |
newSal = (curSal * payInc / 100) + curSal; // pay increase was a %percentage | |
//"fixed notation != e+10" "showpoint" decimall will always display "setpercicion" amount of dec | |
cout << fixed << showpoint << setprecision(2); | |
cout << firstName << " " << lastName << " " << newSal << endl; | |
outData << fixed << showpoint << setprecision(2); | |
outData << firstName << " " << lastName << " " << newSal << endl; | |
} | |
//Step 5)Disassociate stream variables | |
inData.close(); | |
outData.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment