diff options
| -rw-r--r-- | misc/ofstream-issue/ofstream-issue.cpp | 47 | ||||
| -rw-r--r-- | misc/ofstream-issue/report.txt | 3 |
2 files changed, 50 insertions, 0 deletions
diff --git a/misc/ofstream-issue/ofstream-issue.cpp b/misc/ofstream-issue/ofstream-issue.cpp new file mode 100644 index 0000000..094e0dd --- /dev/null +++ b/misc/ofstream-issue/ofstream-issue.cpp @@ -0,0 +1,47 @@ +#include <iostream> +#include <fstream> +#include <typeinfo> + +#define MAXLEN 100 + +using namespace std; + +/* Usando ostream (stream) */ +typedef struct { + int id; + const char name[MAXLEN]; +} student1_t; + +ostream& operator<<(ostream& os, student1_t rhs) { + os << "Student name: " << rhs.name; + return os; +} + +/* Usando oftream (file) */ +typedef struct { + int id; + const char name[MAXLEN]; +} student2_t; + +ofstream& operator<<(ofstream& os, student2_t rhs) { + os << "Student name: " << rhs.name; + return os; +} + +int main() { + ofstream out("report.txt"); + if (!out) { + cerr << "Error: no se pudo abrir el archivo\n"; + return 1; + } + student1_t student1 = {1, "Ana Ambooken"}; + student2_t student2 = {2, "Bob Banana"}; + + out << student1 << "PUCP" << '\n'; // works + out << "PUCP" << student1 << '\n'; // works + + out << student2 << "PUCP" << '\n'; // works + /* out << "PUCP" << student2 << '\n'; // breaks */ + + return 0; +} diff --git a/misc/ofstream-issue/report.txt b/misc/ofstream-issue/report.txt new file mode 100644 index 0000000..54a6273 --- /dev/null +++ b/misc/ofstream-issue/report.txt @@ -0,0 +1,3 @@ +Student name: Ana AmbookenPUCP +PUCPStudent name: Ana Ambooken +Student name: Bob BananaPUCP |
