Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. 60 FILE *fp = fopen(argsInfo.inFileStr,"r");
  2. 61 /*pointer point to the malloc return value*/
  3. 62 char *ptr =NULL;
  4. 63 /*temp string hold the each line from the file*/
  5. 64 char temp[BUFSIZ];
  6. 65 /*if file doesn't exist*/
  7. 66 if(fp==NULL){
  8. 67 /*file doesn't exist error message*/
  9. 68 perror(argsInfo.inFileStr);
  10. 69 /*exit the programme*/
  11. 70 return TRUE;
  12. 71 }
  13. 72 /*read the file line by line*/
  14. 73 while(fgets(temp,BUFSIZ,fp)!=NULL){
  15. 74 /*save pointer for strtok_r*/
  16. 75 char *saveptr;
  17. 76 /*token hold each tokenlized string*/
  18. 77 char *token;
  19. 78 /*for loop to tokenlize the string*/
  20. 79 for(token = strtok_r(temp,STR_FILE_TOK_SEPARATOR,&saveptr);
  21. 80 token!=NULL;
  22. 81 token=strtok_r(NULL,STR_FILE_TOK_SEPARATOR,&saveptr)){
  23. 82 *(dynamicString+index) =
  24. 83 (char*)realloc(*(dynamicString+index),sizeof(char*)*(index+1)); /*dynamic allocate memory for each token*/
  25. 84 ptr = (char*)malloc((strlen(token)+1)*sizeof(char));
  26. 85 /*copy the token the the memory*/
  27. 86 strcpy(ptr,token);
  28. 87 /*assign the pointer the the pointer array*/
  29. 88 *(dynamicString+index)=ptr;
  30. 89 /*increment the index*/
  31. 90 index++;
  32. 91 }
  33. 92 }
  34. 93 }
  35. 94 /*case read from command line*/
  36. 95 else{
  37. 96 /*read from optind un argc*/
  38. 97 while(counter<argc){
  39. 98 *(dynamicString+index)=NULL;
  40. 99 /*realloc the memory for each user input token*/
  41. 100 dynamicString =
  42. 101 (char**)realloc(dynamicString,
  43. 102 sizeof(char**)*(index+1));
  44. 103 /*copy each user input string to the memory space*/
  45. 104 // strcpy(*(dynamicString+index),argv[counter]);
  46. 105 *(dynamicString+index) = argv[counter];
  47. 106 /*increment the counter*/
  48. 107 counter++;
  49. 108 /*increment the index*/
  50. 109 index++;
  51. 110 }
  52. 111 }
  53. 112 /*loop through all the string in the dynamic allocated memory*/
  54. 113 for(counter = 0; counter < index;counter++){
  55. 114 /*if string contain float point*/
  56. 115 if(strchr(dynamicString[counter],'.')==NULL){
  57. 103,1 71%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement