diff options
| author | Mitsuo Tokumori <[email protected]> | 2023-09-19 14:19:25 -0500 |
|---|---|---|
| committer | Mitsuo Tokumori <[email protected]> | 2023-09-19 14:19:25 -0500 |
| commit | a79d2ac6a872cb0b10fd502adbf853b013bcde70 (patch) | |
| tree | fe716ea80b556e659cdb0adffbbeed9363a6ff4a | |
| parent | 14b6efe39936acb81328a965af0da68cf3cc3c86 (diff) | |
| download | LP1-a79d2ac6a872cb0b10fd502adbf853b013bcde70.tar.gz LP1-a79d2ac6a872cb0b10fd502adbf853b013bcde70.tar.bz2 LP1-a79d2ac6a872cb0b10fd502adbf853b013bcde70.zip | |
Make "problem" exaple simpler
| -rw-r--r-- | misc/ofstream-issue/ofstream-issue.cpp | 38 | ||||
| -rw-r--r-- | misc/ofstream-issue/report.txt | 6 |
2 files changed, 18 insertions, 26 deletions
diff --git a/misc/ofstream-issue/ofstream-issue.cpp b/misc/ofstream-issue/ofstream-issue.cpp index 094e0dd..192cbb6 100644 --- a/misc/ofstream-issue/ofstream-issue.cpp +++ b/misc/ofstream-issue/ofstream-issue.cpp @@ -1,30 +1,22 @@ #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; +typedef struct {int i;} custom1_t; +typedef struct {int i;} custom2_t; -ostream& operator<<(ostream& os, student1_t rhs) { - os << "Student name: " << rhs.name; - return os; +/* Usando ostream (stream) (good) */ +ostream& operator<<(ostream& os, custom1_t rhs) { + return os << "stream"; } -/* 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; +/* Usando oftream (file) (bad) */ +ofstream& operator<<(ofstream& os, custom2_t rhs) { + //return os << "file"; /* compiler complains */ + os << "file"; return os; } @@ -34,14 +26,14 @@ int main() { cerr << "Error: no se pudo abrir el archivo\n"; return 1; } - student1_t student1 = {1, "Ana Ambooken"}; - student2_t student2 = {2, "Bob Banana"}; + custom1_t t1 = {0}; // use stream + custom2_t t2 = {0}; // use fstream - out << student1 << "PUCP" << '\n'; // works - out << "PUCP" << student1 << '\n'; // works + out << t1 << "PUCP" << '\n'; + out << "PUCP" << t1 << '\n'; - out << student2 << "PUCP" << '\n'; // works - /* out << "PUCP" << student2 << '\n'; // breaks */ + out << t2 << "PUCP" << '\n'; + // out << "PUCP" << t2 << '\n'; // breaks return 0; } diff --git a/misc/ofstream-issue/report.txt b/misc/ofstream-issue/report.txt index 54a6273..80c1d8d 100644 --- a/misc/ofstream-issue/report.txt +++ b/misc/ofstream-issue/report.txt @@ -1,3 +1,3 @@ -Student name: Ana AmbookenPUCP -PUCPStudent name: Ana Ambooken -Student name: Bob BananaPUCP +streamPUCP +PUCPstream +filePUCP |
