cppdatabase.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 14 May 2009 17:31:45 +0200
changeset 92 0bc2646daa82
parent 90 d494813f9e5b
child 94 c100ba6939e3
permissions -rw-r--r--
RMI Hello Example from Sun Microsystems This is a distributed Hello World Program and fully rmi powered committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
90
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * cppdatabase.cc
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2009 Markus Broeker
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 *
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 * stores data in a file and retrieves it back
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
 */
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <cstdlib>
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <iostream>
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <fstream>
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#define DB_FILE "/tmp/test.dat"
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
struct Person {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    char lastname[80];
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
    char firstname[80];
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    char position[80];
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
};
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
void make_db_entry (std::ofstream& out, struct Person& p)
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
{
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    out.write ((char *)&p, sizeof (Person));
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
}
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
void read_db_entry (std::ifstream& in)
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
{
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    std::cout << "clear eof" << std::endl;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    in.clear ();
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    std::cout << "seek status: " << in.seekg (0, std::ios::beg) <<
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
        std::endl << std::endl;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    struct Person p;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    while ((in.read ((char *)&p, sizeof (Person))) != NULL) {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
        std::cout << p.lastname << ", " << p.firstname << ", " <<
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
            p.position << std::endl;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    }
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
}
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
using namespace std;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
int main ()
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
{
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    struct Person p[] = {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
        {"Ribery", "Franck", "Midfielder"},
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
        {"Lahm", "Phillip", "Defender"},
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        {"Toni", "Luca", "Striker"},
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
        {"Klose", "Miroslav", "Striker"},
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    };
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
    ifstream in;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
    ofstream out;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    out.open (DB_FILE, ios::binary);
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
    if (!out) {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
        cout << "Outputfile Error " << DB_FILE << endl;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
        return EXIT_FAILURE;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
    }
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
    for (unsigned int i = 0; i < sizeof (p) / sizeof (Person); i++) {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
        make_db_entry (out, p[i]);
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
    }
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
    out.close ();
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    in.open (DB_FILE);
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
    if (!in) {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
        cout << "Inputfile Error " << DB_FILE << endl;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
        return EXIT_FAILURE;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
    }
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
    read_db_entry (in);
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
    read_db_entry (in);
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
    in.close ();
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
    return EXIT_SUCCESS;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
}