Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Apple Xcode
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #define BUFFER_SIZE 256
- char *func_date(void)
- {
- // date / time
- time_t the_raw_time;
- struct tm *the_time_info;
- // time: returns the value of time in seconds since the Epoch
- time(&the_raw_time);
- the_time_info = localtime(&the_raw_time);
- // format date / time
- static char the_buffer[BUFFER_SIZE];
- the_time_info = localtime (&the_raw_time);
- strftime(the_buffer, BUFFER_SIZE, "%Y-%m-%d: ", the_time_info);
- return the_buffer;
- }
- int main (int argc, const char * argv[])
- {
- // ficheiro de log
- FILE *the_file;
- size_t the_count;
- // w+ create the file if it does not exist
- the_file = fopen("/Users/.../Desktop/log", "r+");
- if (the_file == NULL)
- {
- perror("erro na abertura do ficheiro");
- return EXIT_FAILURE;
- }
- // file size, put pointer at the eof
- fseek(the_file, 0, SEEK_END);
- long the_file_size = ftell(the_file);
- printf("ficheiro: %ld bytes\n", the_file_size);
- // strings
- char *the_test = func_date();
- char *the_string = "nova entrada.\n";
- // concatenate strings
- strcat(the_test, the_string);
- // output
- the_count = fwrite(the_test, 1, strlen(the_test), the_file);
- printf("escritos: %zu bytes. fclose(fp) %s.\n", the_count, fclose(the_file) == 0 ? "com sucesso" : "falhou");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement