Advertisement
paulogp

Ficheiro de LOG

Jul 13th, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. // Apple Xcode
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8.  
  9. #define BUFFER_SIZE 256
  10.  
  11. char *func_date(void)
  12. {
  13.     // date / time
  14.     time_t the_raw_time;
  15.     struct tm *the_time_info;
  16.  
  17.     // time: returns the value of time in seconds since the Epoch
  18.     time(&the_raw_time);
  19.     the_time_info = localtime(&the_raw_time);
  20.  
  21.     // format date / time
  22.     static char the_buffer[BUFFER_SIZE];
  23.  
  24.     the_time_info = localtime (&the_raw_time);
  25.     strftime(the_buffer, BUFFER_SIZE, "%Y-%m-%d: ", the_time_info);
  26.  
  27.     return the_buffer;
  28. }
  29.  
  30.  
  31. int main (int argc, const char * argv[])
  32. {
  33.     // ficheiro de log
  34.     FILE *the_file;
  35.     size_t the_count;
  36.  
  37.     // w+ create the file if it does not exist
  38.     the_file = fopen("/Users/.../Desktop/log", "r+");
  39.  
  40.     if (the_file == NULL)
  41.     {
  42.         perror("erro na abertura do ficheiro");
  43.         return EXIT_FAILURE;
  44.     }
  45.  
  46.     // file size, put pointer at the eof
  47.     fseek(the_file, 0, SEEK_END);
  48.     long the_file_size = ftell(the_file);
  49.     printf("ficheiro: %ld bytes\n", the_file_size);
  50.  
  51.     // strings
  52.     char *the_test = func_date();
  53.     char *the_string = "nova entrada.\n";
  54.  
  55.     // concatenate strings
  56.     strcat(the_test, the_string);
  57.  
  58.     // output
  59.     the_count = fwrite(the_test, 1, strlen(the_test), the_file);
  60.     printf("escritos: %zu bytes. fclose(fp) %s.\n", the_count, fclose(the_file) == 0 ? "com sucesso" : "falhou");  
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement