Advertisement
Darb420

Assignment6

Nov 26th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <limits.h>
  4.  
  5. int main(int argc, char *argv[]) {
  6.  
  7. char** names;
  8. double** price;
  9. int** quantity;
  10.  
  11. char buff[1024];
  12. int n;
  13. int count=0;
  14. int i;
  15.  
  16.  
  17. FILE *fp;
  18.  
  19.  
  20. if(argc != 3){
  21. printf("Improper execution...\n");
  22. }else{
  23. fp = fopen(argv[1], "r");
  24. if(fp == 0){
  25. printf("Could not open file\n");
  26. }
  27. else {
  28.  
  29. while(fgets(buff, 1024, fp)!=NULL) {
  30. count++;
  31. }
  32. rewind(fp);
  33.  
  34. names = (char**)malloc(count*sizeof(char*));
  35. price = (double**)malloc(count*sizeof(double*));
  36. quantity = (int**)malloc(count*sizeof(int*));
  37. for(i=0; i<count; i++){
  38. names[i] = (char*)malloc(100*sizeof(char));
  39. price[i] = (double*)malloc(sizeof(double));
  40. quantity[i] = (int*)malloc(sizeof(int));
  41. }
  42. n = 0;
  43. while(fgets(buff, 1024, fp) !=NULL) {
  44.  
  45. names[n] = (char*)strtok(buff, ",");
  46. price[n] = (double*)atof((char*)strtok(NULL, ","));
  47. quantity[n] = (int*)atoi((char*)strtok(NULL,"\0"));
  48.  
  49. // sscanf(buff,"%s, %lf, %d", names[n],price[n],quantity[n]);
  50.  
  51. n++;
  52.  
  53.  
  54.  
  55. }
  56. for(i=0;i<count;i++) {
  57.  
  58. printf("%d, %s, %lf, %d", i+1, names[i],price[i],quantity[i]);
  59.  
  60. }
  61.  
  62.  
  63.  
  64.  
  65. fclose(fp);
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement