summaryrefslogtreecommitdiffstats
path: root/doc/html/ksquirrel-libs-fio.html
blob: 2ead0b7a6840e0a992727052b7a42b4d23e64a9e (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>KSquirrel: development</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name='Author' content='Baryshev Dmitry/Krasu'>

    <link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>

For reading and writing data I use the following classes, inherited from ifstream and ofstream.

<table cellpadding="2" cellspacing="2" width="70%" align="center">
<tbody>
<tr>
<td valign="top" bgcolor="#CCCCCC">
<pre>
<b>Class for reading binary data</b>
class ifstreamK : public ifstream
{
    public:
	ifstreamK();

	<b>Read 'size' bytes of binary data and store it into 'data'.
	Returns true if reading was successful, and false otherwise</b>
	bool readK(void *data, int size);

        <b>Read string from file</b>
        bool getS(char *, const int);

        <b>Read ascii hex value from file (like "0xFFFF00")</b>
        bool readCHex(u32 &amp;hex);

	<b>big-endian-oriented reading</b>
	bool be_getchar(u8 *c);
	bool be_getshort(u16 *s);
	bool be_getlong(u32 *l);
};

<b>Class for writing binary data</b>
class ofstreamK : public ofstream
{
    public:
	ofstreamK();

	bool writeK(void *data, int size);
};
</pre>
</td>
</tr>
</tbody>
</table>

<b><u>Examples</u></b>

<ul>
<li> Read structure from binary file
<br><br>
<table cellpadding="2" cellspacing="2" width="70%" align="center">
  <tbody>
  <tr>
  <td valign="top" bgcolor="#CCCCCC">

  <pre>
     typedef struct
     {
         int a;
         short b;
         char c;

     }Data;

     ...

     class::class()
     {
         fs.open("file.bin", ios::in | ios::binary);
     }

     int class::readstruct(Data *s)
     {
         bool b = fs.readK(s, sizeof(Data));
	
         cerr &#60;&#60; "Reading " &#60;&#60; (b ? "OK" : "Failed") &#60;&#60; endl;
     }

  </pre>
  </td>
  </tr>
  </tbody>
</table>

<br><br>
<li> Write structure to binary file
<br><br>
<table cellpadding="2" cellspacing="2" width="70%" align="center">
  <tbody>
  <tr>
  <td valign="top" bgcolor="#CCCCCC">

  <pre>
     typedef struct
     {
         int a;
         short b;
         char c;

     }Data;

     ...

     class::class()
     {
         fs.open("file.bin", ios::out | ios::binary);
     }

     int class::writestruct(Data *s)
     {
         bool b = fs.writeK(s, sizeof(Data));

         cerr &#60;&#60; "Writing " &#60;&#60; (b ? "OK" : "Failed") &#60;&#60; endl;
     }

  </pre>
  </td>
  </tr>
  </tbody>
</table>

<br>

<b><u>Note:</u></b> remember, that reading and writing structures in binary mode is not doog idea. This is just example. See <a href='http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#7'>http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#7</a> and <a href='http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#3'>http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#3</a> for more.

<br><br>

</ul>
</body>
</html>