APPFS
Advanced practical programming for scientists
 All Data Structures Files Functions Variables Typedefs Macros
ex1a.c
Go to the documentation of this file.
1 
10 //#include <sys/types.h>
11 #include <stdio.h> // printf
12 //#include <stdlib.h>
13 #include <stdbool.h> // bool
14 #include <limits.h> // INT_MAX
15 #include <unistd.h> // read
16 #include <fcntl.h> // O_RDONLY
17 #include <string.h> // memset
18 #include <assert.h> // assert
19 
20 #define MAX_NUMS ((INT_MAX >> 5) + 1)
21 #define BUF_NUMS 4096
22 
23 
31 int main(int argc, char** argv)
32 {
33  const char* usage = "usage: %s filename\n";
34 
35  /* made static, because otherwise, the stack size might be too small.
36  */
37  static unsigned int have_num[MAX_NUMS];
38 
39  int buf[BUF_NUMS];
40  int fd;
41  int n;
42  int total_nums = 0;
43 
44  /* we assume 32 bit integers, otherwise it will not work
45  */
46  assert(sizeof(int) == 4);
47 
48  /* Check arguments, we need a filename.
49  */
50  if (argc < 2)
51  {
52  fprintf(stderr, usage, argv[0]);
53  exit(EXIT_FAILURE);
54  }
55  memset(have_num, 0, sizeof(have_num)); // Proably uneccessary
56 
57  /* Open file
58  */
59  if (0 > (fd = open(argv[1], O_RDONLY)))
60  {
61  perror(argv[1]);
62  exit(EXIT_FAILURE);
63  }
64 
65  /* Read from file until data exhausted
66  */
67  while(0 < (n = read(fd, buf, sizeof(buf))))
68  {
69  int nums_read = n / sizeof(buf[0]);
70 
71  assert(nums_read <= BUF_NUMS);
72 
73  total_nums += nums_read;
74 
75  /* Run through buffer of read numbers and set mark bits
76  */
77  for(int i = 0; i < nums_read; i++)
78  {
79  // fprintf(stderr, "%d\n", buf[i]);
80 
81  /* Check input: really >= 0 ? Otherwise ignore
82  */
83  if (buf[i] >= 0)
84  {
85  int idx = buf[i] >> 5; // n / 32
86  unsigned int msk = 1 << (buf[i] & 31); // bit number n mod 32
87 
88  assert(idx >= 0 && idx < MAX_NUMS);
89 
90  have_num[idx] |= msk;
91  }
92  }
93  }
94  if (close(fd))
95  {
96  perror("close: ");
97  exit(EXIT_FAILURE);
98  }
99  fprintf(stderr, "Total numbers read = %d\n", total_nums);
100 
101  for(int i = 0; i < MAX_NUMS; i++)
102  if (have_num[i]) // just for speed up
103  for(int k = 0; k < 31; k++)
104  if (have_num[i] & (1 << k))
105  printf("%d\n", (i << 5) + k);
106 
107  return EXIT_SUCCESS;
108 }
#define BUF_NUMS
read buffer size
Definition: ex1a.c:21
int main(int argc, char **argv)
Program to read a file with binary positive 32 bit integers, sort the numbers and print them sorted...
Definition: ex1a.c:31
NUMS * nums_read(const char *filename)
Read numbers into data structure.
Definition: nums.c:52
#define MAX_NUMS
Maximum number of 32 bit ints we need to store bits.
Definition: ex1a.c:20