From 869e68986aa8f69af6e7842260a68d1e5c6f796f Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:24:20 +0200 Subject: Add a bunch of code Signed-off-by: Carlos Maiolino --- CPP/Basics/files.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 CPP/Basics/files.cpp (limited to 'CPP/Basics/files.cpp') 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 +#include +#include + +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; +} -- cgit v1.2.3