Advertisement
tpaper

Untitled

Jan 28th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #define DIM 1000
  2. #include <stdio.h>
  3.  
  4. int carica(FILE *fp, char str[], int max){
  5.     int i=0;
  6.     while( (feof(fp) == 0) && (i < max-1) ) {   //continuo fino a eof o fino a buffer pieno
  7.         fscanf(fp,"%c",&(str[i++]));
  8.     }
  9.     str[i] = '\0';  //metto un terminatore alla stringa
  10.     if (i >= max-1) return 1;
  11.     return 0;
  12. }
  13.  
  14. void stampa(char *str){
  15.     int i = 0;
  16.     while(str[i] != '\0') printf("%c",str[i++]); //stampo tutto fino al terminatore
  17. }
  18.  
  19. int main() {
  20.     char buffer[DIM];
  21.    
  22.     FILE *fp = fopen("test.txt", "r");
  23.    
  24.     if (fp == NULL) {
  25.         printf("Errore nell'apertura del file\n");
  26.         return 1;
  27.     }
  28.    
  29.     if (carica(fp,buffer,DIM) == 1) printf("Buffer di lettura esaurito!\n\n"); else printf("Tutto il file letto correttamente!\n\n");
  30.     stampa(buffer);
  31.     printf("\n");
  32.    
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement