Skip to content

Instantly share code, notes, and snippets.

@residentkrm
Created September 10, 2015 21:10
Show Gist options
  • Save residentkrm/68d533cd0b9b02e7162d to your computer and use it in GitHub Desktop.
Save residentkrm/68d533cd0b9b02e7162d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdio>
#include <fstream>
#include <vector>
#include <conio.h>
int main()
{
// prepare file
std::ofstream("d:/test.txt") << 1 << ' ' << 2 << '\n';
std::FILE* f;
fopen_s(&f, "d:/test.txt", "r");
std::vector<char> buf(4); // char is trivally copyable
std::fread(&buf[0], sizeof buf[0], buf.size(), f);
for (char n : buf)
std::cout << n;
std::vector<std::string> buf2; // string is not trivially copyable
// this would result in undefined behavior
// std::fread(&buf2[0], sizeof buf2[0], buf2.size(), f);
_getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment