Skip to content

Instantly share code, notes, and snippets.

@rocket-fuel86
Created May 11, 2026 13:07
Show Gist options
  • Select an option

  • Save rocket-fuel86/e04268291bf2e30ebd3247e4caaa70af to your computer and use it in GitHub Desktop.

Select an option

Save rocket-fuel86/e04268291bf2e30ebd3247e4caaa70af to your computer and use it in GitHub Desktop.
Fractions in binary file
#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);
}
}
#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