Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. /* This Program generates a file with a pseudo-random number of st_record_t structures. The file is passed by command line arguments. The program must by executed, in UNIX, this way: ./file_gen -path <path> */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include "types.h"
  8.  
  9. #define MSG_INVALID_INPUT "Your input was not valid"
  10.  
  11. #define CMD_FLAG_PATH_POSITION 1
  12. #define CMD_ARG_PATH_POSITION 2
  13.  
  14. #define CMD_FLAG_PATH "-path"
  15.  
  16. #define SDM_MAX 10000.0
  17.  
  18. status_t validate_arguments (int argc, char * argv []);
  19.  
  20. int main (int argc, char * argv [])
  21. {
  22. FILE * fi;
  23. size_t i;
  24. st_record_t aux_struct, aux2_struct;
  25. int size;
  26.  
  27. if ((validate_arguments(argc, argv))!= OK)
  28. {
  29. fprintf(stderr, "%sn", MSG_INVALID_INPUT);
  30. return EXIT_FAILURE;
  31. }
  32.  
  33. if((fi = fopen(argv[CMD_ARG_PATH_POSITION], "wb")) == NULL)
  34. return EXIT_FAILURE;
  35.  
  36. srand(time(NULL));
  37. for (i=0; i<(size=100); i++)
  38. {
  39. aux_struct.SDM = (((float)rand()/(float)(RAND_MAX)) * SDM_MAX); /*pseudo-random real number between 0 and SDM_MAX*/
  40. (aux_struct.ID) = i;
  41. (aux_struct.coordinates)->latitude.deg = rand()%180;
  42. (aux_struct.coordinates)->latitude.min = rand()%60;
  43. (aux_struct.coordinates)->latitude.sec = rand()%60;
  44. (aux_struct.coordinates)->longitude.deg = rand()%180;
  45. (aux_struct.coordinates)->longitude.min = rand()%60;
  46. (aux_struct.coordinates)->longitude.sec = rand()%60;
  47. if((fwrite (&aux_struct, sizeof(st_record_t), 1, fi))!=1)
  48. return ERROR_WRITING_FILE;
  49. }
  50.  
  51. if(fclose(fi) == EOF)
  52. return EXIT_FAILURE
  53.  
  54. return EXIT_SUCCESS;
  55. }
  56.  
  57. typedef struct {
  58. unsigned char deg, min, sec;
  59. }angle_t;
  60.  
  61. typedef struct {
  62. angle_t latitude, longitude;
  63. }st_coord_t;
  64.  
  65. typedef struct {
  66. float SDM;
  67. size_t ID;
  68. st_coord_t * coordinates;
  69. }st_record_t;
  70.  
  71. aux_struct.coordinates = malloc(sizeof(st_coord_t));
  72.  
  73. typedef struct {
  74. float SDM;
  75. size_t ID;
  76. st_coord_t * coordinates;
  77. }st_record_t;
  78.  
  79. aux_struct.coordinates=malloc(sizeof(st_coord_t));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement