This repository was archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd2f.c
More file actions
50 lines (40 loc) · 1.16 KB
/
d2f.c
File metadata and controls
50 lines (40 loc) · 1.16 KB
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
/*
* Convert double binary data in float binary data
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define BUFFER 4096
int main(int argc, char *argv[])
{
if(argc != 1) {
fprintf(stderr, "Convert double to float binary file\n");
fprintf(stderr, "\nExample:\n");
fprintf(stderr, " $ %s < doubledataset.bin > floatdataset.bin\n", argv[0]);
fprintf(stderr, "\nAlso can be used in a pipe as:\n");
fprintf(stderr, " $ cat doubledataset.bin | %s > floatdataset.bin \n", argv[0]);
exit(1);
}
if(isatty(fileno(stdin))) {
fprintf(stderr, "STDIN must be a 'pipe' or 'file'\n");
exit(1);
}
if(isatty(fileno(stdout))) {
fprintf(stderr, "STDOUT must be a 'pipe' or 'file'\n");
exit(1);
}
/* file buffers */
double bufin[BUFFER];
float bufout[BUFFER];
size_t bytesread;
while((bytesread = fread(bufin, sizeof(double), BUFFER, stdin)))
{
for(int i=0; i<BUFFER; i++)
bufout[i] = bufin[i];
fwrite(bufout, sizeof(float), bytesread, stdout);
}
fflush(stdin);
fflush(stdout);
return 0;
}