Advertisement
AntonioVillanueva

TOKENS desde fichero strtok

Jan 20th, 2023
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. /*Separar por Tokens con strtok A.Villanueva
  2. * char *strtok(char *str, const char *delim)
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #define FICHERO "cameConfig.txt"
  10. #define LONGITUD_MAX 64
  11.  
  12. int main() {
  13.    
  14.     FILE *fptr;
  15.        
  16.     char * token;
  17.     const char * sep = " ,.-!\t";//Separadores
  18.     char linea[LONGITUD_MAX]; //Linea  
  19.    
  20.     //Apertura del fichero
  21.     if( (fptr = fopen(FICHERO,"r")) == NULL)//Abre el fichero en lectura
  22.     {
  23.       printf("Error! %s",FICHERO);  
  24.       exit(1);            
  25.     }  
  26.    
  27.        
  28.     while (fgets(linea, LONGITUD_MAX, fptr)){ //Lee linea
  29.        
  30.         token = strtok ( linea, sep );//Primer token
  31.  
  32.         while( token != NULL ) {//Recorro los tokens
  33.             printf( " -> %s\n", token );
  34.             token = strtok(NULL, sep);
  35.         }  
  36.     }
  37.    
  38.    
  39.     fclose(fptr);//Cierro fichero
  40.    
  41.     return EXIT_SUCCESS;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement