Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Le header .h:
- #ifndef Source_H
- #define Source_H
- void lectTxt();
- #endif
- Le Source.cpp :
- #include "stdafx.h"
- #include "Source.h"
- #include <stdlib.h>
- void lectTxt(){
- int index= 0;
- char tampon;
- char *tab;
- /* première allocation de mémoire pour "tab",
- verification d'allocation de mémoire;
- première aquisition de caractère,
- inscription du caractère a l'index dans le tableau "tab"*/
- tab = (char*) malloc( 1 * sizeof(char));
- if(tab == NULL)
- {
- printf("Erreur! memoire non allouee");
- fflush(stdin);
- getchar();
- exit(0);
- }
- tampon = getchar();
- tab[index] = tampon;
- /*Boucle while
- incrémentation de l'index,
- allocation mémoire
- aquisition du caractère
- inscription du caractère a l'index dans le tableau "tab"
- */
- while (tampon != '\n')
- {
- index++;
- tab = (char*) realloc(tab,(index+1) * sizeof(char));
- tampon = getchar();
- tab[index] = tampon;
- }
- /*cloture du texte par "\0"
- affichage erreur si aucun tesxte entré sinon affichage du texte */
- tab[index+1] = '\0';
- if (tab[1] == '\0')
- {
- printf("Erreur pas de texte");
- }else
- {
- printf("\nVotre texte :\n%s", tab);
- }
- }
- Le main :
- #include "stdafx.h"
- #include "Source.h"
- #include <stdlib.h>
- int _tmain(int argc, _TCHAR* argv[])
- {
- printf("Entrez votre texte32\n");
- //appel de la fonction
- lectTxt();
- fflush(stdin);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement