Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <strings.h>
  3.  
  4. #define MAXLINE 200
  5. #define MAXROW 50
  6. #define MAXCOLUMN 50
  7.  
  8. // This is the based row structure
  9. // it contains an array of data elements
  10. typedef struct row_struct{
  11.     char* data[MAXCOLUMN];
  12.     int max_width;
  13.     int column_amount;
  14. }Row_t;
  15.  
  16. // A table consists of rows and columns
  17. // and the largest line in the table
  18. typedef struct table_struct{
  19.     Row_t rows[MAXROW];
  20.     int max_output_width;
  21.     int row_amount;
  22. }Table_t;
  23.  
  24. int main(int argc, char* argv[]){
  25.     // Yes,this is ANSI c compliant
  26.     int i,j,token_length;
  27.     char* key_column_name;
  28.     char line[MAXLINE];
  29.     char* token;
  30.     Table_t* table;
  31.     FILE* in_fileptr;
  32.     FILE* out_fileptr;
  33.     char delims[] = {'\t', '\n'};
  34.    
  35.     // First we want to verify the proper number of arguments
  36.     if (argc != 4){
  37.         printf("Invalid command line arguments usage: tablelookup <input_file>");
  38.         printf(" <output_file> <column>");
  39.     }
  40.     // File pointers to both the input and output as well as the key column
  41.     in_fileptr = fopen(argv[1],"r");
  42.     out_fileptr = fopen(argv[2],"w");
  43.     key_column_name = argv[3];
  44.    
  45.     // Our inital table (max 50x50) I suppose a dymanic array
  46.     // structure could have been designed but this is quite a
  47.     // small program to do that
  48.     table = malloc(sizeof(Table_t));
  49.    
  50.     // Read line by line into new rows inside the table
  51.         i = 0;
  52.         while (fgets(line,MAXLINE,in_fileptr) != NULL){
  53.             // A new row is added
  54.             ++(table->row_amount);
  55.             // Token the line in column data
  56.             printf("\n line: %s",line);
  57.             token = strtok(line,delims);
  58.             for (j = 0; token != NULL; ++j){
  59.                 // A new column is added
  60.                 ++(table->rows[i].column_amount);
  61.                 // Check if the token is the max width
  62.                 token_length = strlen(token);
  63.                 if (token_length > table->rows[i].max_width ){
  64.                     table->rows[i].max_width = token_length;
  65.                 }
  66.                 // Allocate memory for that string and add the data
  67.                 // to the row
  68.                 (table->rows[i]).data[j] = malloc(sizeof((strlen(token))+1));
  69.                 (table->rows[i]).data[j] = token;
  70.                 printf("token: %s--",token);
  71.                 printf("slot %d %d has %s\n",i,j,table->rows[i].data[j]);
  72.                 printf("slot 0 0 now has %s\n",table->rows[0].data[0]);
  73.                 // Obtain a new token from the line
  74.                 token = strtok(NULL,delims);
  75.             }  
  76.             ++i;
  77.         }
  78.     // Close the file
  79.     fclose(in_fileptr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement