Created
May 11, 2026 13:07
-
-
Save rocket-fuel86/e04268291bf2e30ebd3247e4caaa70af to your computer and use it in GitHub Desktop.
Fractions in binary file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include "main.h" | |
| int main() { | |
| FILE* file; | |
| if (const auto path = "data.bin"; fopen_s(&file, path, "wb+") == 0) { | |
| constexpr Fraction fractions[] = { | |
| {2, 3}, | |
| {1, 2}, | |
| { 3, 5}, | |
| }; | |
| fwrite(fractions, sizeof(fractions), 1, file); | |
| fseek(file, 0, SEEK_SET); | |
| Fraction read[3]; | |
| fread(read, sizeof(read), 1, file); | |
| printFractionArray(read, 3); | |
| fclose(file); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef HW44_MAIN_H | |
| #define HW44_MAIN_H | |
| struct Fraction { | |
| int numerator; | |
| int denominator; | |
| void print() const { | |
| std::cout << numerator << "/" << denominator << std::endl; | |
| } | |
| }; | |
| inline void printFractionArray(Fraction frac[], size_t length) { | |
| for (size_t i = 0; i < length; i++) { | |
| frac[i].print(); | |
| } | |
| } | |
| #endif //HW44_MAIN_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment