Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <limits.h>
- int main(int argc, char *argv[]) {
- char** names;
- double** price;
- int** quantity;
- char buff[1024];
- int n;
- int count=0;
- int i;
- FILE *fp;
- if(argc != 3){
- printf("Improper execution...\n");
- }else{
- fp = fopen(argv[1], "r");
- if(fp == 0){
- printf("Could not open file\n");
- }
- else {
- while(fgets(buff, 1024, fp)!=NULL) {
- count++;
- }
- rewind(fp);
- names = (char**)malloc(count*sizeof(char*));
- price = (double**)malloc(count*sizeof(double*));
- quantity = (int**)malloc(count*sizeof(int*));
- for(i=0; i<count; i++){
- names[i] = (char*)malloc(100*sizeof(char));
- price[i] = (double*)malloc(sizeof(double));
- quantity[i] = (int*)malloc(sizeof(int));
- }
- n = 0;
- while(fgets(buff, 1024, fp) !=NULL) {
- names[n] = (char*)strtok(buff, ",");
- price[n] = (double*)atof((char*)strtok(NULL, ","));
- quantity[n] = (int*)atoi((char*)strtok(NULL,"\0"));
- // sscanf(buff,"%s, %lf, %d", names[n],price[n],quantity[n]);
- n++;
- }
- for(i=0;i<count;i++) {
- printf("%d, %s, %lf, %d", i+1, names[i],price[i],quantity[i]);
- }
- fclose(fp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement