Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #define FILE_PATH "/tmp/anyfile"
- struct record {
- uint8_t id;
- uint8_t type;
- uint8_t source;
- char body[32];
- };
- int main ()
- {
- struct record any_record = {.id = 2, .type = 4, .source = 33, .body = "Hello"};
- uint16_t another_stuff = 12; // Different compiler have different implementation of int, however uint16_t mitigites such problem
- FILE* that_file;
- // Blank line: seperates the declaration and program
- that_file = fopen(FILE_PATH, "w");
- if (NULL == that_file) {
- // why NULL == that_file?
- // if the program made a typo of "NULL = that_file", it works actually
- printf("Sorry, failed to open file.\n");
- return -1;
- }
- // put an record to the file
- fwrite(&any_record, sizeof(any_record), 1, that_file);
- fflush(that_file);
- fclose(that_file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement