Advertisement
Guest User

Lecture texte

a guest
May 3rd, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. Le header .h:
  2.  
  3. #ifndef Source_H
  4. #define Source_H
  5. void lectTxt();
  6. #endif
  7.  
  8. Le Source.cpp :
  9.  
  10. #include "stdafx.h"
  11. #include "Source.h"
  12. #include <stdlib.h>
  13.  
  14. void lectTxt(){
  15.  
  16.     int index= 0;
  17.     char tampon;
  18.  
  19.     char *tab;
  20.  
  21.  
  22.     /* première allocation de mémoire pour "tab",
  23.     verification d'allocation de mémoire;
  24.     première aquisition de caractère,
  25.     inscription du caractère a l'index dans le tableau "tab"*/
  26.  
  27.     tab = (char*) malloc( 1 * sizeof(char));
  28.  
  29.     if(tab == NULL)                    
  30.     {
  31.         printf("Erreur! memoire non allouee");
  32.         fflush(stdin);
  33.         getchar(); 
  34.  
  35.         exit(0);
  36.     }
  37.  
  38.  
  39.     tampon = getchar();
  40.     tab[index] = tampon;
  41.  
  42.  
  43.     /*Boucle while
  44.     incrémentation de l'index,
  45.     allocation mémoire
  46.     aquisition du caractère
  47.     inscription du caractère a l'index dans le tableau "tab"
  48.     */
  49.  
  50.     while (tampon != '\n')
  51.     {
  52.  
  53.  
  54.         index++;
  55.  
  56.         tab = (char*) realloc(tab,(index+1) * sizeof(char));
  57.  
  58.         tampon = getchar();
  59.  
  60.         tab[index] = tampon;
  61.  
  62.     }
  63.  
  64.     /*cloture du texte par "\0"
  65.     affichage erreur si aucun tesxte entré sinon affichage du texte */
  66.     tab[index+1] = '\0';
  67.  
  68.     if (tab[1] == '\0')
  69.     {
  70.         printf("Erreur pas de texte");
  71.     }else
  72.     {
  73.         printf("\nVotre texte :\n%s", tab);
  74.     }
  75.  
  76. }
  77.  
  78. Le main :
  79.  
  80. #include "stdafx.h"
  81. #include "Source.h"
  82. #include <stdlib.h>
  83.  
  84.  
  85. int _tmain(int argc, _TCHAR* argv[])
  86. {
  87.    
  88.    
  89.    
  90.    
  91.    
  92.     printf("Entrez votre texte32\n");
  93.    
  94.     //appel de la fonction
  95.     lectTxt();
  96.  
  97.    
  98.    
  99.     fflush(stdin);
  100.     getchar(); 
  101.    
  102.    
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement