Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Converting" from int[] to std::array at fread?

Tags:

c++

std

c++11

I was wondering and have not found any advice, on how to convert a C-style integer array to a new std::array from C++11

I love the std::array and use it a lot, anyhow if I read in data from binary files, I work around to copy the values. Isn't there a simpler way when using fread?

FILE* fp; // initalize not important here
int arr[12];
std::array<int,12> stdarr;
fread(arr, sizeof(int), 12, fp);
for(int i=0; i < 12; i++)
stdarr[i] = arr[i];
like image 967
Stefan Avatar asked Feb 06 '26 01:02

Stefan


1 Answers

Reading into the address of the first element array's underlying data block should work:

fread(stdarr.data(), sizeof(int), stdarr.size(), fp);
like image 140
juanchopanza Avatar answered Feb 08 '26 15:02

juanchopanza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!