Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // arbitrary buffer size for reading file input
  5. #define BUFSIZE 1000
  6.  
  7. void find_array_maxes(int *input_numbers){
  8. //int max = 0;
  9. printf("%d\n", *input_numbers);
  10. while(*input_numbers != '\0'){
  11.  
  12. //if(max < *input_numbers){
  13. // max = *input_numbers;
  14. //}
  15. input_numbers++;
  16. }
  17. //return max;
  18. }
  19.  
  20. int main(int argc, char *argv[]){
  21. // check to ensure there are sufficient command line arguments
  22. if(argc <= 2){
  23. printf("At least two arguments are required!\n");
  24. exit(1);
  25. } else {
  26. // allocates memory for array that will store file values
  27. int array_size = atoi(argv[1]);
  28. int *array_ptr = (int*) malloc(array_size * sizeof(int));
  29.  
  30. // creates a file ptr and buffer to read file contents
  31. FILE *file = fopen(argv[2], "r");
  32. char buff[BUFSIZE];
  33. int converted_num;
  34. int counter = 0;
  35.  
  36. // verify the array is allocated
  37. if(array_ptr == NULL){
  38. printf("Memory allocation of %d bytes has failed.\n", *array_ptr);
  39. fclose(file);
  40. exit(1);
  41. } else {
  42. // stores contents of file into the buffer and is converted from the buffer to int
  43. // to be stored in the dynamically allocated array
  44. while(fgets(buff, BUFSIZE-1, file) != NULL && counter < array_size){
  45. converted_num = atoi(buff);
  46. *array_ptr = converted_num;
  47. printf("%d\n", *array_ptr);
  48. array_ptr++;
  49. counter++;
  50. }
  51. fclose(file);
  52. }
  53. find_array_maxes(array_ptr);
  54. //printf("The max number of the array is %d\n",find_array_maxes(array_ptr));
  55. }
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement