Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.10 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include <string.h>
  5.  
  6.  
  7. int maxLines;
  8. int main(int argc, char *argv[]) {
  9.    
  10.     if(argc != 3){
  11.         printf("Improper execution...\n");
  12.     }
  13.    
  14.     // Good command sent.
  15.     else{
  16.  
  17.      //   fileReader(argv[1]);
  18.  
  19.         char delims[] = {"\n"};
  20.         char *result = NULL;
  21.         int i = 0, j;
  22.         char sent[1000];
  23.         char *gameArray[1024];//32 word
  24.        
  25.         strcpy(sent, argv[1]);//strtok destory string, argv[n] is read only.
  26.        
  27.         char *buffer = NULL;
  28.         int string_size,read_size;
  29.         FILE *handler = fopen(argv[1],"r");
  30.        
  31.        
  32.        
  33.        
  34.         // Handles the reading of the file and printing for the first time.
  35.         // This also saves each NEWLINE (i.e) each game to the parray
  36.         if (handler)
  37.         {
  38.             //find the last byte of file
  39.             fseek(handler,0,SEEK_END);
  40.            
  41.             //create string size
  42.             string_size = (int)ftell(handler);
  43.            
  44.             //go back to the start of the file
  45.             rewind(handler);
  46.            
  47.             //allocate a string that can hold it all
  48.             buffer = (char*) malloc (sizeof(char) * (string_size + 1) );
  49.            
  50.             //Read entire file
  51.             read_size = fread(buffer,sizeof(char),string_size,handler);
  52.            
  53.             //End the buffer with NULL character.
  54.             //This buffer now contains a very long string with all the contents of the
  55.             //passed games file.
  56.             buffer[string_size] = '\0';
  57.        
  58.            
  59.             //We will tokenize the long buffer string here with the NEWLINE '\n' as a delimiter
  60.             result = strtok(buffer, delims);
  61.             //Save new line i.e new game to next index of the array.
  62.             gameArray[i++] = result;
  63.            
  64.            
  65.             while(NULL!= (result=strtok(NULL, delims))){
  66.                 gameArray[i++] = result;
  67.             }
  68.            
  69.             // Initial Print of File List Happens here
  70.             for(j=0; j < i;++j)
  71.                 printf("%s\n", gameArray[j]);
  72.             maxLines = j;
  73.         }// End IF
  74.     }// End Else
  75.    
  76.    
  77.    
  78.     // AT THIS POINT YOU HAVE AN ARRAY OF GAMES WITH EACH LINE REPRESENTING A GAME
  79.    
  80.     // WHAT YOU NEED TO DO IS TOKENIZE ON ',' AND SAVE THAT TO AN ARRAY OF NAME, PRICES AND AN
  81.     // ARRAY OF QUANITY
  82.    
  83.     // SO THAT YOU CAN THEN REQUEST INPUT FROM THE USER. IE IF THEY SAY 2, THEN YOU WOULD GO TO
  84.     // PRICES[2] = PRICES[2]-1 or something like that ....
  85.    
  86.    
  87.    
  88.    
  89.    
  90.    
  91.    
  92.     // ****** TRYING TO GET THIS WORKING ****** //
  93.     int n;
  94.     while(1){
  95.         scanf ("%d",&n);
  96.         char exit = (char)n;
  97.         if(exit == 'q'){
  98.             printf("Bye...");
  99.             break;
  100.         }
  101.         else if(n < 0 || n > maxLines){
  102.             printf("ERROR Not Line Matches...");
  103.             break;
  104.         }
  105.         else{
  106.             printf("Lets Handel the commands here");
  107.             break;
  108.         }
  109.     }
  110.         return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement