diff options
| author | Carlos Maiolino <[email protected]> | 2025-07-10 22:24:20 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-07-10 22:24:20 +0200 |
| commit | 869e68986aa8f69af6e7842260a68d1e5c6f796f (patch) | |
| tree | 63b6b5ffc3d19414233d4629a533c0d9bf3cbf72 /CPP/Basics/files.cpp | |
| parent | 20834dcc57537cd95260a4a22f5d91a027adfd35 (diff) | |
Add a bunch of code
Signed-off-by: Carlos Maiolino <[email protected]>
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; +} |
