cppdatabase.cc
author Markus Brökers <mbroeker@largo.homelinux.org>
Wed, 18 Aug 2010 18:17:51 +0200
changeset 143 88d9b8bfa7a5
parent 94 c100ba6939e3
child 169 df7c720bcaa6
permissions -rw-r--r--
a md5 demo is already in lsflib/tools
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 ();
94
c100ba6939e3 cppdatabase: removed the useless status message
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 90
diff changeset
    29
    in.seekg (0, std::ios::beg);
90
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    struct Person p;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    while ((in.read ((char *)&p, sizeof (Person))) != NULL) {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
        std::cout << p.lastname << ", " << p.firstname << ", " <<
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
            p.position << std::endl;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    }
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
using namespace std;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
int main ()
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
{
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    struct Person p[] = {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
        {"Ribery", "Franck", "Midfielder"},
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
        {"Lahm", "Phillip", "Defender"},
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
        {"Toni", "Luca", "Striker"},
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        {"Klose", "Miroslav", "Striker"},
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
    };
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
    ifstream in;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
    ofstream out;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
    out.open (DB_FILE, ios::binary);
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    if (!out) {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        cout << "Outputfile Error " << DB_FILE << endl;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
        return EXIT_FAILURE;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    }
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
    for (unsigned int i = 0; i < sizeof (p) / sizeof (Person); i++) {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
        make_db_entry (out, p[i]);
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
    }
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
    out.close ();
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
    in.open (DB_FILE);
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    if (!in) {
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
        cout << "Inputfile Error " << DB_FILE << endl;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
        return EXIT_FAILURE;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    }
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
    read_db_entry (in);
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
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
    in.close ();
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
    return EXIT_SUCCESS;
d494813f9e5b cppdatabase added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
}