Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <strings.h>
- #define MAXLINE 200
- #define MAXROW 50
- #define MAXCOLUMN 50
- // This is the based row structure
- // it contains an array of data elements
- typedef struct row_struct{
- char* data[MAXCOLUMN];
- int max_width;
- int column_amount;
- }Row_t;
- // A table consists of rows and columns
- // and the largest line in the table
- typedef struct table_struct{
- Row_t rows[MAXROW];
- int max_output_width;
- int row_amount;
- }Table_t;
- int main(int argc, char* argv[]){
- // Yes,this is ANSI c compliant
- int i,j,token_length;
- char* key_column_name;
- char line[MAXLINE];
- char* token;
- Table_t* table;
- FILE* in_fileptr;
- FILE* out_fileptr;
- char delims[] = {'\t', '\n'};
- // First we want to verify the proper number of arguments
- if (argc != 4){
- printf("Invalid command line arguments usage: tablelookup <input_file>");
- printf(" <output_file> <column>");
- }
- // File pointers to both the input and output as well as the key column
- in_fileptr = fopen(argv[1],"r");
- out_fileptr = fopen(argv[2],"w");
- key_column_name = argv[3];
- // Our inital table (max 50x50) I suppose a dymanic array
- // structure could have been designed but this is quite a
- // small program to do that
- table = malloc(sizeof(Table_t));
- // Read line by line into new rows inside the table
- i = 0;
- while (fgets(line,MAXLINE,in_fileptr) != NULL){
- // A new row is added
- ++(table->row_amount);
- // Token the line in column data
- printf("\n line: %s",line);
- token = strtok(line,delims);
- for (j = 0; token != NULL; ++j){
- // A new column is added
- ++(table->rows[i].column_amount);
- // Check if the token is the max width
- token_length = strlen(token);
- if (token_length > table->rows[i].max_width ){
- table->rows[i].max_width = token_length;
- }
- // Allocate memory for that string and add the data
- // to the row
- (table->rows[i]).data[j] = malloc(sizeof((strlen(token))+1));
- (table->rows[i]).data[j] = token;
- printf("token: %s--",token);
- printf("slot %d %d has %s\n",i,j,table->rows[i].data[j]);
- printf("slot 0 0 now has %s\n",table->rows[0].data[0]);
- // Obtain a new token from the line
- token = strtok(NULL,delims);
- }
- ++i;
- }
- // Close the file
- fclose(in_fileptr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement