diff options
Diffstat (limited to 'CPP/Basics/files.cpp')
| -rw-r--r-- | CPP/Basics/files.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/CPP/Basics/files.cpp b/CPP/Basics/files.cpp new file mode 100644 index 0000000..1242224 --- /dev/null +++ b/CPP/Basics/files.cpp @@ -0,0 +1,66 @@ +#include <iostream> +#include <string> +#include <fstream> + +using namespace std; + +void read_from_file() +{ + ifstream myFile; + string str, str2; + int number; + char letter; + + myFile.open("people.txt"); + + if (myFile.fail()) { + cout << endl << "File not found!" << endl; + } else { + while (!myFile.eof()) { + getline(myFile, str); + cout << str << ", "; + getline(myFile, str); + number = stoi(str); + cout << number << ", "; + getline(myFile, str); + cout << str << endl; + } + myFile.close(); + } +} + +void write_to_file() +{ + ofstream outFile; + float a = 4.333f; + float b = 5.302f; + + outFile.open("calc.txt"); + if (outFile.fail()) { + cout << endl << "Couldn't open the file!" << endl; + } else { + outFile << "Ronaldo" << endl; + outFile << "25" << endl; + outFile << "J" << endl; + outFile << "Cretino" << endl; + outFile << "23" << endl; + outFile << "C" << endl; + outFile << "Mano dos Pano" << endl; + outFile << "23" << endl; + outFile << "M" << endl; + outFile.close(); + cout << "File written successfully!" << endl; + + } +} +int main(void) +{ + + write_to_file(); + read_from_file(); + + // Reading a whole line from the terminal +// getline(cin, str2); +// cout << str2 << endl; + return 0; +} |
