APPFS
Advanced practical programming for scientists
 All Data Structures Files Functions Variables Typedefs Macros
Functions
ex1b3.c File Reference

Appfs Exercise 1: use of subroutines. More...

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "nums.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 Program to read a file with binary positive 32 bit integers, sort the numbers and print them sorted. More...
 

Detailed Description

Author
Thorsten Koch
Date
18Oct2014

gcc -O3 -Wall -std=c99 -o ex1b3 ex1b3.c nums.c

Use stream file for input

Definition in file ex1b3.c.

Function Documentation

int main ( int  argc,
char **  argv 
)

Actually, the numbers are not sorted. There is an array with one bit for each possible number. Upon reading the respective bit is set for the number recognized. At the end the array is scanned in order and the numbers present are printed.

Parameters
argv[1] name of file to read

Definition at line 24 of file ex1b3.c.

25 {
26  const char* usage = "usage: %s filename\n";
27 
28  /* Check arguments, we need a filename.
29  */
30  if (argc < 2)
31  {
32  fprintf(stderr, usage, argv[0]);
33  exit(EXIT_FAILURE);
34  }
35 
36  /* we assume 32 bit integers, otherwise it will not work
37  */
38  assert(sizeof(int) == 4);
39 
40  NUMS* nsp = nums_read(argv[1]);
41 
42  if (NULL == nsp)
43  return EXIT_FAILURE;
44 
45  fprintf(stderr, "Total numbers read = %zu\n", nums_used(nsp));
46 
47  nums_print(nsp);
48 
49  nums_free(nsp);
50 
51  return EXIT_SUCCESS;
52 }
Data structure to store numbers.
Definition: nums.c:19
void nums_free(NUMS *nsp)
Free NUMS data structure.
Definition: nums.c:141
NUMS * nums_read(const char *filename)
Read numbers into data structure.
Definition: nums.c:52
void nums_print(const NUMS *nsp)
Sort and print all numbers in the NUMS structure.
Definition: nums.c:166
size_t nums_used(const NUMS *nsp)
Return count of numbers in NUMS structure.
Definition: nums.c:187