summaryrefslogtreecommitdiffstats
path: root/misc/ofstream-issue/ofstream-issue.cpp
blob: 192cbb6785fff1d40bef0c0d36591b826d49995a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>

#define MAXLEN 100

using namespace std;

typedef struct {int i;} custom1_t;
typedef struct {int i;} custom2_t;

/* Usando ostream (stream) (good) */
ostream& operator<<(ostream& os, custom1_t rhs) {
    return os << "stream";
}

/* Usando oftream (file) (bad) */
ofstream& operator<<(ofstream& os, custom2_t rhs) {
    //return os << "file";        /* compiler complains */
    os << "file";
    return os;
}

int main() {
    ofstream out("report.txt");
    if (!out) {
        cerr << "Error: no se pudo abrir el archivo\n";
        return 1;
    }
    custom1_t t1 = {0};       // use stream
    custom2_t t2 = {0};       // use fstream

    out << t1 << "PUCP" << '\n';
    out << "PUCP" << t1 << '\n';

    out << t2 << "PUCP" << '\n';
    // out << "PUCP" << t2 << '\n';  // breaks

    return 0;
}