Advertisement
sapitando

fgets e stdin.

Sep 14th, 2016
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. /* Autor : Tiago Portela
  2.    Email : sapitando@gmail.com
  3.    Sobre : Compilado com TDM-GCC 5.10 64-bit e LCC-Win32 64-bit;
  4.    Obs : Apenas tentando aprender algoritimos, sozinho, por hobby. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include <string.h>
  10. #include <assert.h>
  11.  
  12. #define BUFFERSIZE 8
  13.  
  14. typedef struct _Node {
  15.     char *lpBuffer;
  16.     struct _Node *LpProxNode;
  17. } Node_t, *LpNode_t;
  18.  
  19. int main(void)
  20. {
  21.     char acBuffer[BUFFERSIZE] = {0};
  22.     LpNode_t lpNode = (LpNode_t)malloc(sizeof(Node_t));
  23.     assert(lpNode!=NULL);
  24.     LpNode_t lpHeadNode = lpNode;
  25.     char* lpBuffer = (char*)calloc(1,sizeof(char));
  26.     assert(lpBuffer!=NULL);
  27.     char cChar;
  28.  
  29.  
  30.     printf("Enter a message: \n");
  31.     // Exit when Enter is pressed
  32. /*  while(true)
  33.     {
  34.         assert(fgets(acBuffer, BUFFERSIZE, stdin)!=NULL);
  35.         lpNode->lpBuffer = malloc((strlen(acBuffer) + 1) * sizeof(char));
  36.         assert(lpNode->lpBuffer!=NULL);
  37.         strcpy(lpNode->lpBuffer, acBuffer);
  38.         if(lpNode->lpBuffer[strlen(acBuffer) - 1] == '\n')
  39.         {
  40.             lpNode->lpBuffer[strlen(acBuffer) - 1] = '\0';
  41.             lpNode->LpProxNode = NULL;
  42.             break;
  43.         }
  44.         lpNode->LpProxNode = (LpNode_t)malloc(sizeof(Node_t));
  45.         lpNode = lpNode->LpProxNode;
  46.         assert(lpNode!=NULL);
  47.     }*/
  48.  
  49.     // Exits the loop if the line is empty(Improving code).
  50.     while(true)
  51.     {
  52.         assert(fgets(acBuffer, BUFFERSIZE, stdin)!=NULL);
  53.         lpNode->lpBuffer = malloc((strlen(acBuffer) + 1) * sizeof(char));
  54.         assert(lpNode->lpBuffer!=NULL);
  55.         strcpy(lpNode->lpBuffer, acBuffer);
  56.         if(acBuffer[strlen(acBuffer) - 1] == '\n')
  57.             lpNode->lpBuffer[strlen(acBuffer) - 1] = '\0';
  58.         if(strlen(acBuffer) == (BUFFERSIZE - 1) && (acBuffer[strlen(acBuffer) - 1] != '\n'))
  59.         {
  60.             cChar = fgetc(stdin);
  61.             if(cChar != '\n')
  62.                 ungetc(cChar, stdin);
  63.         }
  64.         if(acBuffer[0] == '\n')
  65.         {
  66.             lpNode->LpProxNode = NULL;
  67.             break;
  68.         }
  69.         lpNode->LpProxNode = (LpNode_t)malloc(sizeof(Node_t));
  70.         lpNode = lpNode->LpProxNode;
  71.         assert(lpNode!=NULL);
  72.     }
  73.  
  74.  
  75.     printf("\nPseudo String :\n");
  76.     lpNode = lpHeadNode;
  77.     while(lpNode != NULL)
  78.     {
  79.         printf("%s", lpNode->lpBuffer);
  80.         lpNode = lpNode->LpProxNode;
  81.     }
  82.  
  83.  
  84.     printf("\n\nMemory blocks:\n");
  85.     lpNode = lpHeadNode;
  86.     while(lpNode != NULL)
  87.     {
  88.         printf("Block \"%7s\" size = %lu\n", lpNode->lpBuffer, (long unsigned)(strlen(lpNode->lpBuffer) + 1));
  89.         lpNode = lpNode->LpProxNode;
  90.     }
  91.  
  92.  
  93.     printf("\nConcatenated string:\n");
  94.     lpNode = lpHeadNode;
  95.     while(lpNode != NULL)
  96.     {
  97.         lpBuffer = (char*)realloc(lpBuffer, (strlen(lpBuffer) + strlen(lpNode->lpBuffer)) + 1);
  98.         strcat(lpBuffer, lpNode->lpBuffer);
  99.         lpNode = lpNode->LpProxNode;
  100.     }
  101.     printf("%s", lpBuffer);
  102.     printf("\n\n");
  103.  
  104.     // Deallocate memory
  105.     lpNode = lpHeadNode;
  106.     while(lpNode != NULL)
  107.     {
  108.         lpHeadNode = lpNode->LpProxNode;
  109.         free(lpNode->lpBuffer);
  110.         free(lpNode);
  111.         lpNode = lpHeadNode;
  112.     }
  113.     lpBuffer = (char*)realloc(lpBuffer, 0);
  114.     lpBuffer = NULL;
  115.     if((lpNode == NULL) && (lpBuffer == NULL))
  116.     {
  117.  
  118.         printf("Deallocate memory = %s", (char*)lpNode);
  119.     }
  120.     printf("\n\n");
  121.  
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement