rrz0

fscanf() unexpected return

Dec 20th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.94 KB | None | 0 0
  1. //#pragma warning(disable:4996)
  2. // C program to implement basic operations (search, insert and delete)
  3. // on a BST that handles duplicates by storing count with every node
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #define MAX_STRING_SIZE 100
  9. #define HASHSIZE 101  /* comment here */
  10. #define FILE_OUTPUT "calendar.txt"
  11. #define ALLOCSIZE 1000 /*size of available space*/
  12. #define N 1000 // max number of lines to be read
  13. #define VALLEN 100 //max number of chars for description
  14.  
  15.  
  16. static char allocbuf[ALLOCSIZE]; /* storage for alloc*/
  17. static char *allocp = allocbuf; /* next free position*/
  18. static struct map *hashtab[HASHSIZE]; /* pointer table */
  19. unsigned hash(char *); /* hashing function: form hash value for string s. used by both lookup and install*/
  20. char *strdup(char *);
  21.  
  22. char *alloc(int n) { /* return a pointer to n characters*/
  23.     if (allocbuf + ALLOCSIZE - allocp >= n) { /*it fits*/
  24.         allocp += n;
  25.         return allocp - n; /*old p*/
  26.     } else /*not enough room*/
  27.         return 0;
  28. }
  29.  
  30. typedef struct map { /* creating a map structure */
  31.     struct map *next;
  32.     char *value;
  33.     char *KEY;
  34. } map;
  35.  
  36. typedef struct node {
  37.     int KEY;
  38.     char *value;
  39.     int node_count;
  40.     struct node *left, *right;
  41.     int unique_count;
  42. } node;
  43.  
  44.  
  45. // A utility function to create a new BST node
  46.  
  47. node *newNode(int KEY, char *value) {
  48.     //Allocate a new overall structure
  49.     node *temp = (node *) malloc(sizeof (node));
  50.  
  51.     //Copy the integer key
  52.     temp->KEY = KEY;
  53.     temp->value = value;
  54.  
  55.     temp->left = temp->right = NULL;
  56.     temp->node_count = 1;
  57.     return temp;
  58. }
  59.  
  60. // A utility function to do inorder traversal of BST
  61.  
  62. void inorder(node *root, int *pin_count) {
  63.     if (root != NULL) {
  64.         inorder(root->left, pin_count);
  65.         //printf("%d(%d)\n", root->KEY, root->node_count);
  66.         inorder(root->right, pin_count);
  67.     }
  68. }
  69.  
  70. /* A utility function to find deepest keys of BST */
  71.  
  72. /* This is done by extracting the key with the lowest count,
  73. since count member of node struct gets incremented each time it is read. */
  74. void unique_key(node *root, int *p_unique_count, int p_unique_key_arr[], char *p_value_arr[]) {
  75.     int count = 0;
  76.     char *p;
  77.  
  78.     if (root != NULL) {
  79.         unique_key(root->left, p_unique_count, p_unique_key_arr, p_value_arr);
  80.         if (root->node_count == 1) {
  81.  
  82.             if ((p = alloc(strlen(root[count].value) + 1)) == NULL) {
  83.                 return;
  84.             } else {
  85.                 root[count].unique_count = *p_unique_count;
  86.                 p_unique_key_arr[*p_unique_count] = root[count].KEY;
  87.  
  88.                 strcpy(p, root[count].value);
  89.                 p_value_arr[*p_unique_count] = p;
  90.                 //printf("%s\n",p_value_arr[*p_unique_count][*p_unique_count]);
  91.  
  92.                 //printf("%d(%d) -> %s %d\n", root->KEY, root->node_count, root->value, root->unique_count);
  93.                 (*p_unique_count)++;
  94.                 count++;
  95.             }
  96.         }
  97.         unique_key(root->right, p_unique_count, p_unique_key_arr, p_value_arr);
  98.     }
  99. }
  100.  
  101. /* A utility function to insert a new node with given key in BST */
  102. node* insert_node(node* node, int key, char *value) {
  103.     /* If the tree is empty, return a new node */
  104.     if (node == NULL)
  105.         return newNode(key, value);
  106.  
  107.     // If key already exists in BST, icnrement count and return
  108.     if (key == node->KEY) {
  109.         (node->node_count)++;
  110.         //  return node;
  111.     }
  112.  
  113.     /* Otherwise, recur down the tree */
  114.     if (key < node->KEY)
  115.         node->left = insert_node(node->left, key, value);
  116.     else
  117.         node->right = insert_node(node->right, key, value);
  118.  
  119.     /* return the (unchanged) node pointer */
  120.     return node;
  121. }
  122.  
  123. /* lookup function takes a pointer to char - s as an argument and returns a pointer to map structure */
  124. map *lookup(char *s) {
  125.     map *np;
  126.  
  127.     for (np = hashtab[hash(s)]; np != NULL; np = np->next) {
  128.         if (strcmp(s, np->KEY) == 0) {
  129.             return np;
  130.         }
  131.     }
  132.     return NULL;
  133. }
  134.  
  135. /* insert function takes a pointer to a char - KEY, value and returns a pointer to map structure */
  136. map *insert_map(char* KEY, char* value) {
  137.     /* install: put (name, defn) in */
  138.     /* Install uses lookup to determine whether the KEY being installed
  139.        is already present. Proceeds to create a new entry or update*/
  140.     map *np;
  141.     unsigned hashval;
  142.  
  143.     if ((np = lookup(KEY)) == NULL) {
  144.         np = (map *) malloc(sizeof (*np));
  145.         if (np == NULL || (np->KEY = strdup(KEY)) == NULL) {
  146.             return NULL;
  147.         }
  148.         hashval = hash(KEY);
  149.         np->next = hashtab[hashval];
  150.         hashtab[hashval] = np;
  151.     } else {
  152.         free((void *) np->value);
  153.     }
  154.     if ((np->value = strdup(value)) == NULL) { //map has no field value
  155.         return NULL;
  156.     }
  157.  
  158.     return np;
  159. }
  160.  
  161. /* uninstall function removes a key and calendar from table maintained by lookup and install */
  162. map *remove_map(char *KEY) {
  163.     map *found;
  164.  
  165.     found = lookup(KEY);
  166.  
  167.     if (found == NULL) /* not found and nothing to do */ {
  168.         printf("%s - Nothing to delete.\n", KEY);
  169.         return NULL;
  170.     } else {
  171.         if (found->next != NULL) {
  172.             found->next = found->next->next;
  173.             found = found->next;
  174.         } else {
  175.             hashtab[hash(KEY)] = NULL;
  176.             free((void *) found);
  177.         }
  178.     }
  179.     return found;
  180. }
  181.  
  182. /* simple implementation of strdup*/
  183. char *strdup(char *s) {
  184.  
  185.     char *d = malloc(strlen(s) + 1); // Space for length plus null
  186.     if (d == NULL) return NULL; // No memory
  187.     strcpy(d, s); // Copy the characters
  188.     return d; // Return the new string
  189. }
  190.  
  191. unsigned hash(char *s) {
  192.     /* hashing function: form hash value for string s */
  193.     /* used by both lookup and install*/
  194.     unsigned hashval;
  195.  
  196.     for (hashval = 0; *s != '\0'; s++) {
  197.         hashval = *s + 31 * hashval;
  198.     }
  199.     return hashval % HASHSIZE;
  200. }
  201.  
  202. // Driver Program to test above functions
  203.  
  204. int main() {
  205.     FILE *f_cal;
  206.     //FILE *outp;
  207.     int total_unique = 0;
  208.     int in_count, count_create = 0;
  209.     int count_delete = 0;
  210.     int unique_key_arr[10]; // Make dynamic
  211.     char *value_arr[10];
  212.     char *VAL_arr_create[10]; // an array of pointers [unique][chars]
  213.     char *VAL_arr_delete[10];
  214.     char *KEY_arr_create[10];
  215.     char *KEY_arr_delete[10];
  216.  
  217.     f_cal = fopen("calendar.txt", "r+");
  218.  
  219.  
  220.     int key_arr[N], a, count = 0;
  221.     char val_arr[N][VALLEN], b[10], c[10], d[10];
  222.  
  223.     if (f_cal) {
  224.         printf("Processing file...\n");
  225.         int ret = fscanf(f_cal, "%d %s %s %s", &a, b, c, d);
  226.         printf("%d\n", ret);
  227.         if (ret == 4) {
  228.             while (fscanf(f_cal, "%d %s %s %s", &a, b, c, d) == 4) {
  229.                 if (count < N) {
  230.                     key_arr[count] = a;
  231.                     snprintf(val_arr[count++], VALLEN, "%s %s %s", b, c, d);
  232.                 }
  233.             }
  234.             for (int i = 0; i < count; ++i)
  235.                 printf("key=%d, val = %s\n", key_arr[i], val_arr[i]);
  236.         } else {
  237.             printf("File is empty.\n");
  238.         }
  239.         fclose(f_cal);
  240.     } else {
  241.         printf("no file found.\n");
  242.     }
  243.  
  244.     //unique *data = NULL;
  245.  
  246.     /* READ CALENDER_OPERATIONS*/
  247.     /* Pass all operations down to BST*/
  248.  
  249.     /* Let us create following BST.  Passing values along with key */
  250.     node *root = NULL;
  251.     //node *root = (node*)malloc(sizeof(node));
  252.     node *test = (node*) malloc(sizeof (node));
  253.  
  254.     //this is for storing commands
  255.     root = insert_node(root, 2082018, "C 1200 79 Meeting");
  256.     root = insert_node(root, 3082018, "C 1300 60 Lunch");
  257.     root = insert_node(root, 4082018, "C 1400 30 Dinner");
  258.     root = insert_node(root, 5082018, "C 0600 90 Work");
  259.     root = insert_node(root, 16082018, "D 4300 30 Diy");
  260.     root = insert_node(root, 7082018, "C 5608 30 Dinner");
  261.     root = insert_node(root, 18082018, "C 1409 35 to");
  262.     root = insert_node(root, 29082018, "C 1600 60 play");
  263.     root = insert_node(root, 9082018, "U 1800 88 e");
  264.     root = insert_node(root, 9082018, "D 1800 88 e");
  265.  
  266.     printf("Inorder traversal of the given tree \n");
  267.     inorder(root, &in_count);
  268.  
  269.     printf("\nDeepest unique keys of the given tree \n");
  270.     unique_key(root, &total_unique, unique_key_arr, value_arr);
  271.     printf("\n");
  272.  
  273.     printf("\nDeepest unique keys of the given tree in main\n");
  274.     for (int i = 0; i < total_unique; i++) {
  275.         printf("%d [%s]\n", unique_key_arr[i], value_arr[i]);
  276.         if (*value_arr[i] == 'C') { //Compare first char of pointer type
  277.         } else if (*value_arr[i] == 'U') { //Compare first char of pointer type
  278.         } else if (*value_arr[i] == 'D') { //Compare first char of pointer type
  279.         }
  280.     }
  281.     printf("\n");
  282.  
  283.     /* Map is for storing calendar. This should contain the unique elements
  284.      * from the BST above. Based on the first char inside `value_arr`, we
  285.      * should insert or delete a map. Number of operations to perform
  286.       depends on variable `unique_count`. Use strcpy()*/
  287.  
  288.     printf("\nDeepest unique keys to map structure: %d\n", total_unique);
  289.     for (int unique_count = 0; unique_count < total_unique; unique_count++) {
  290.         char* p_val; //pointer to char for value
  291.         char* p_key; //pointer to char for key
  292.         p_key = malloc(sizeof (char) + 1); //this is malloced to access different memory each time.
  293.  
  294.         if (*value_arr[unique_count] == 'C' || *value_arr[unique_count] == 'U') { //Compare first char of pointer type
  295.  
  296.             if ((p_val = alloc(strlen(value_arr[unique_count]) + 1)) == NULL) {
  297.                 return -1;
  298.             } else {
  299.                 strcpy(p_val, value_arr[unique_count]); //copying value to pointer to char
  300.                 VAL_arr_create[count_create] = p_val; //p passed to value array
  301.             }
  302.  
  303.             //Convert integer to pointer to char
  304.             sprintf(p_key, "%d", unique_key_arr[unique_count]);
  305.             KEY_arr_create[count_create] = p_key; //array containing all create/updates
  306.  
  307.             printf("I am here %s, %s\n", KEY_arr_create[count_create], VAL_arr_create[count_create]);
  308.             count_create++;
  309.  
  310.         } else { //must be a delete
  311.  
  312.             if ((p_val = alloc(strlen(value_arr[unique_count]) + 1)) == NULL) {
  313.                 return -1;
  314.             } else {
  315.                 strcpy(p_val, value_arr[unique_count]); //copying value to pointer to char
  316.                 VAL_arr_delete[count_delete] = p_val; //p passed to value array
  317.             }
  318.  
  319.             //Convert integer to pointer to char
  320.             sprintf(p_key, "%d", unique_key_arr[unique_count]);
  321.             KEY_arr_delete[count_delete] = p_key; //array containing all deletes
  322.             printf("I am here2 %s, %s\n", KEY_arr_delete[count_delete], VAL_arr_delete[count_delete]);
  323.             count_delete++;
  324.         }
  325.     }
  326.  
  327.     map * table[10]; //pointer to type map structure
  328.  
  329.     printf("\nBefore insert CREATE %d\n", count_create);
  330.  
  331.     for (int i = 0; i < count_create; i++) {
  332.         printf("CREATE %s, %s\n", KEY_arr_create[i], VAL_arr_create[i]);
  333.         /* Here I need to remove leading operation letter to display*/
  334.         char *p;
  335.         char *copy_val_create = strchr(VAL_arr_create[i], ' ');
  336.         if (copy_val_create)
  337.             printf("COPY: %s\n", copy_val_create + 1);
  338.         else
  339.             printf("Could not find a space\n");
  340.         table[i] = insert_map(KEY_arr_create[i], copy_val_create + 1);
  341.     }
  342.  
  343.     printf("\nBefore insert DELETE %d\n", count_delete);
  344.     for (int i = 0; i < count_delete; i++) {
  345.         printf("DELETE %s, %s\n", KEY_arr_delete[i], VAL_arr_delete[i]);
  346.         remove_map(KEY_arr_delete[i]);
  347.     }
  348.  
  349.     printf("\nUPDATED CALENDAR\n");
  350.     for (int i = 0; i < count_create; i++) {
  351.         printf("%s->%s \n", table[i]->KEY, table[i]->value);
  352.         fprintf(f_cal, "%s %s\n", table[i]->KEY, table[i]->value);
  353.     }
  354.     fclose(f_cal);
  355.     printf("\n");
  356.  
  357.     //remove_map("4082018");
  358.  
  359.     return 0;
  360. }
Advertisement
Add Comment
Please, Sign In to add comment