-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_fwrite.c.1
296 lines (195 loc) · 8.18 KB
/
c_fwrite.c.1
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
//
// fwrite - binary output
//
// sizeof(char) = 1
// sizeof(int) = 4
// sizeof(long) = 8
// sizeof(float) = 4
// sizeof(double) = 8
//
void c_fwrite_astr_ ( FILE **fp, const char *astr, size_t *n, size_t *m )
{
size_t nc = (size_t) ( (*n) * sizeof(char) ) ;
size_t mc = (size_t) 1 ;
// printf ( "nc = %ld \t mc = %ld \n\n", nc, mc ) ;
*m = fwrite ( astr, nc, mc, *fp );
}
//
void c_fwrite_afloat_ ( FILE **fp, const float *ar, size_t *n, size_t *m )
{
size_t nc = (size_t) ( (*n) * sizeof(float) ) ;
size_t mc = (size_t) 1 ;
// printf ( "nc = %ld \t mc = %ld \n\n", nc, mc ) ;
*m = fwrite ( ar, nc, mc, *fp );
}
//
void c_fwrite_adouble_ ( FILE **fp, const double *ad, size_t *n, size_t *m )
{
size_t nc = (size_t) ( (*n) * sizeof(double) ) ;
size_t mc = (size_t) 1 ;
*m = fwrite ( ad, nc, mc, *fp );
}
//
void c_fwrite_aint_ ( FILE **fp, const int *ai, size_t *n, size_t *m )
{
size_t nc = (size_t) ( (*n) * sizeof(int) ) ;
size_t mc = (size_t) 1 ;
*m = fwrite ( ai, nc, mc, *fp );
}
//
void c_fwrite_along_ ( FILE **fp, const long *al, size_t *n, size_t *m )
{
size_t nc = (size_t) ( (*n) * sizeof(long) ) ;
size_t mc = (size_t) 1 ;
*m = fwrite ( al, nc, mc, *fp );
}
////////////////////////////////////////////////////////////////////////////////
//
// New version:
// Pass arguments using its reference with value in integer(8), i.e.
// intptr_t defined in /usr/include/stdint.h
//
#include <stdint.h>
void c_fwrite_astr_intref_ (
intptr_t *iptr, const char *astr, size_t *n, size_t *m )
{
size_t nc = (size_t) ( (*n) * sizeof(char) ) ;
size_t mc = (size_t) 1 ;
*m = fwrite ( astr, nc, mc, (FILE *) (*iptr) );
}
//
void c_fwrite_afloat_intref_ (
intptr_t *iptr, const float *ar, size_t *n, size_t *m )
{
size_t nc = (size_t) ( (*n) * sizeof(float) ) ;
size_t mc = (size_t) 1 ;
*m = fwrite ( ar, nc, mc, (FILE *) (*iptr) );
}
//
void c_fwrite_adouble_intref_ (
intptr_t *iptr, const double *ad, size_t *n, size_t *m )
{
size_t nc = (size_t) ( (*n) * sizeof(double) ) ;
size_t mc = (size_t) 1 ;
*m = fwrite ( ad, nc, mc, (FILE *) (*iptr) );
}
//
void c_fwrite_aint_intref_ (
intptr_t *iptr, const int *ai, size_t *n, size_t *m )
{
size_t nc = (size_t) ( (*n) * sizeof(int) ) ;
size_t mc = (size_t) 1 ;
*m = fwrite ( ai, nc, mc, (FILE *) (*iptr) );
}
//
void c_fwrite_along_intref_ (
intptr_t *iptr, const long *al, size_t *n, size_t *m )
{
size_t nc = (size_t) ( (*n) * sizeof(long) ) ;
size_t mc = (size_t) 1 ;
*m = fwrite ( al, nc, mc, (FILE *) (*iptr) );
}
////////////////////////////////////////////////////////////////////////////////
/*
#include <stdio.h>
/usr/include/stdio.h
+ fwrite - binary output
size_t fwrite ( const void *ptr, size_t size, size_t nitems, FILE *stream );
The fwrite () function shall write, from the array pointed to by PTR, up
to NITEMS elements whose size is specified by SIZE, to the stream pointed
to by STREAM. For each object, the number SIZE of calls shall be made to the
fputc()
function, taking the values (in order) from an array of unsigned char exactly
overlaying the object. The file-position indicator for the stream (if defined)
shall be advanced by the number of bytes successfully written. If an error
occurs, the resulting value of the file-position indicator for the stream is
unspecified.
The last data modification and last file status change timestamps of the
file shall be marked for update between the successful execution of fwrite ()
and the next successful completion of a call to fflush() or fclose() on the
same stream, or a call to exit() or abort().
RETURN VALUE
The fwrite () function shall
return the number
of elements successfully written, which may be less than NITEMS if a write
error is encountered. If SIZE or NITEMS is 0, fwrite () shall
return 0
and the state of the stream remains unchanged. Otherwise, if a write error
occurs, the error indicator for the stream shall be set, and ERRNO shall be
set to indicate the error.
+ fputc - put a byte on a stream
int fputc ( int c, FILE *stream )
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fputc.html
The fputc() function shall write the byte specified by C
(Note: C is integer (4-bytes). It then is converted to an unsigned char),
i.e.
- signed char range -127 to 127. (-128 to 127 is common) (1 byte)
- unsigned char range 0 to 255 (1 byte?)
- char stands for both signed or unsigned. (1 byte)
==> C is converted to integer (4-bytes). Not optimized!
Why dont we use integer(kind=2) in Fortran? OK?
==> Use ICHAR in Fortran for conversion
Name Argument Return type Standard
ICHAR(C) CHARACTER(1) C INTEGER(4) Fortran 77 and later
int fputc ( ICHAR(c), FILE *stream )
to the output stream pointed to by STREAM, at the position indicated by
the associated file-position indicator for the stream (if defined),
and shall advance the indicator appropriately. If the file cannot support
positioning requests, or if the STREAM was opened with append mode,
the byte shall be appended to the output stream.
The last data modification and last file status change timestamps
of the file shall be marked for update between the successful execution
of fputc() and the next successful completion of a call to fflush() or
fclose() on the same stream or a call to exit() or abort().
RETURN VALUE
Upon successful completion, fputc() shall return
the value it has written.
Otherwise, it shall return
EOF,
the error indicator for the stream shall be set, and ERRNO shall be set
to indicate the error.
+ fputs - put a string on a stream
int fputs ( const char *s, FILE *stream )
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fputs.html
The fputs() function shall write the NULL-terminated string pointed
to by S to the stream pointed to by STREAM. The terminating NULL byte
shall not be written.
The last data modification and last file status change timestamps of
the file shall be marked for update between the successful execution of
fputs() and the next successful completion of a call to fflush() or
fclose() on the same stream or a call to exit() or abort().
RETURN VALUE
Upon successful completion, fputs() shall return
a non-negative number
Otherwise, it shall return
EOF,
set an error indicator for the stream, and set errno to indicate the error.
EXAMPLES
Printing to Standard Output
The following example gets the current time, converts it to a
string using localtime() and asctime(), and prints it to standard
output using fputs(). It then prints the number of minutes to an
event for which it is waiting.
#include <time.h>
#include <stdio.h>
time_t now ;
int minutes_to_event ;
time ( &now );
printf ( "The time is " ) ;
fputs ( asctime(localtime(&now)), stdout ) ;
printf ( "There are still %d minutes to the event.\n",
minutes_to_event ) ;
+ putc - put a byte on a stream
int putc ( int c, FILE *stream );
https://pubs.opengroup.org/onlinepubs/9699919799/functions/putc.html
+ puts - put a string on standard output
int puts ( const char *s );
https://pubs.opengroup.org/onlinepubs/9699919799/functions/puts.html
+ putchar - put a byte on a stdout stream
int putchar(int c);
https://pubs.opengroup.org/onlinepubs/9699919799/functions/putchar.html
+ fprintf
int fprintf ( FILE *stream, const char *format, ... );
*/