GatoBooster

Clase --/10/2017

Nov 7th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //FILE *fopen(char *nombre, char *modo);//funcion para abrir o crear archivos.
  4. //int fclose (FILE *stream); //Funcion para cerrar el archivo retorna 0 si se cerro exitosamente o EOF si hay algun error.
  5. int cuentaA (FILE *);
  6. int main (void){
  7.     FILE *fp;
  8.     fp = fopen("entrada.txt","r");
  9.     if(fp ==  NULL){
  10.         printf("ARCHIVO EN CANA, PURA VENDISUONES");
  11.         return 0;
  12.     }
  13.     printf("Total de letras a = %d",cuentaA(fp));
  14.     int ret = fclose(fp);
  15.     if(ret == EOF){
  16.         printf("ERROR AL CERRAR ARCHIVO");
  17.         return EXIT_FAILURE;
  18.     }
  19.     return 0;
  20. }
  21. int cuentaA (FILE *fp){
  22.     int cont=0;
  23.     char caracter;
  24.     while((caracter=fgetc(fp)) != EOF){
  25.         if(caracter == 'a'){
  26.             cont++;
  27.         }
  28.     }
  29.     return cont;
  30. }
  31.  
  32.  
  33.  
  34.  
  35.  
  36. |||||||||||||||FGETS|||||||||||||||||||||||||||
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. void imprimirAPantalla(FILE *entrada);
  40. int main (void){
  41.     FILE *entrada;
  42.     entrada=fopen("entrada.txt","r");
  43.     if(entrada == NULL){
  44.         printf("Errooooooooor......");
  45.         return EXIT_SUCCESS;
  46.     }
  47.     imprimirAPantalla(entrada);
  48.     int ret=fclose(entrada);
  49.    
  50.     if(ret == EOF){
  51.         printf ("Errooooor");
  52.         return EXIT_FAILURE;
  53.     }
  54.     return 0;
  55. }
  56.  
  57. void imprimirAPantalla(FILE *entrada){
  58.     char cadena[80];
  59.     while(fgets(cadena,80,entrada) != NULL){
  60.         printf("%s",cadena);
  61.     }
  62. }
  63.  
  64.  
  65. ||||||||||||||| FSCANF ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  66.  
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69.  
  70. void abrirArchivo(FILE **);
  71. int procesarDatos(FILE *);
  72. void cerrarArchivo(FILE *);
  73. int main (void){
  74.     FILE *entrada;
  75.    
  76.     abrirArchivo(&entrada);
  77.     printf ("suma = %d",procesarDatos(entrada));
  78.     cerrarArchivo(entrada);
  79.    
  80.     return 0;
  81. }
  82.  
  83. void abrirArchivo (FILE **ptr){
  84.     *ptr = fopen ("numeros.txt","r");
  85.     if(*ptr == NULL){
  86.         printf("ERROR");
  87.         exit(0);
  88.     }
  89. }
  90.  
  91. int procesarDatos (FILE *entrada){
  92.     int suma=0;
  93.     int numero;
  94.     while(fscanf(entrada,"%d",&numero) != EOF){
  95.         suma=suma+numero;
  96.     }
  97.     return suma;
  98. }
  99.  
  100. void cerrarArchivo(FILE *entrada){
  101.     int ret=fclose(entrada);
  102.     if(ret == EOF){
  103.         printf ("ERROR");
  104.         exit(1);
  105.     }
  106. }
  107.  
  108.  
  109. |||||||||||||||||||||||||||||||FPUTC |||||||||||||||||||||||||||||  copia de archivos
  110.  
  111. #include <stdio.h>
  112. #include <stdlib.h>
  113. #include <ctype.h>
  114. int main () {
  115.     FILE * entrada;
  116.     FILE * salida;
  117.     entrada=fopen ("entrada.txt","r");
  118.     if(entrada == NULL) {
  119.         printf ("ERROR");
  120.         return 0;
  121.     }
  122.    
  123.     salida=fopen("copia.txt","w");
  124.     if (salida == NULL){
  125.         fclose(entrada);
  126.         printf ("Error");
  127.         return 0;
  128.     }
  129.    
  130.     char caracter;
  131.    
  132.     while((caracter = fgetc(entrada)) != EOF){
  133.         fputc(toupper(caracter),salida);
  134.     }
  135.    
  136.     fclose(entrada);
  137.     fclose(salida);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment