Advertisement
cheungtifan

Untitled

Nov 9th, 2011
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #define FILE_PATH "/tmp/anyfile"
  5.  
  6. struct record {
  7.     uint8_t id;
  8.     uint8_t type;
  9.     uint8_t source;
  10.     char body[32];
  11. };
  12.  
  13. int main ()
  14. {
  15.     struct record any_record = {.id = 2, .type = 4, .source = 33, .body = "Hello"};
  16.     uint16_t another_stuff = 12; // Different compiler have different implementation of int, however uint16_t mitigites such problem
  17.     FILE* that_file;
  18.  
  19.     // Blank line: seperates the declaration and program
  20.     that_file = fopen(FILE_PATH, "w");
  21.     if (NULL == that_file) {
  22.         // why NULL == that_file?
  23.         // if the program made a typo of "NULL = that_file", it works actually
  24.         printf("Sorry, failed to open file.\n");
  25.         return -1;
  26.     }
  27.  
  28.     // put an record to the file
  29.     fwrite(&any_record, sizeof(any_record), 1, that_file);
  30.     fflush(that_file);
  31.     fclose(that_file);
  32.     return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement