C++ Tutorial: Storing records in binary files // Use structs to read and write records

preview_player
Показать описание
In this tutorial, I'll show you how to use structures to to store fixed-length records in binary files. This will work for both single records and arrays of records.

1:37 - Coding Example

// Learn More //
Check out my files playlist:

Check out my playlist of all C++ videos:

Check out my C++ structures playlist:
Рекомендации по теме
Комментарии
Автор

Bruh!!
You're an angel, it's like 4 am and i've been all day trying to do this. Thank you so much!!!
I have a question though.
How does the struct write into the file? Is the name separated from the age and gpa with spaces, \n or not separated at all?
and in between them? From stu[0] to stu[1] for example?
Anyways thanks for the great video, it's a great help to start the study of it. Simple and direct. Awesome explanation.

afelar
Автор

I can not thank you enough for this. Our book does not cover writing array structures, so I had no idea I needed to remove the &. I was agonizing for HOURS.

stephaniepeacock
Автор

im getting a weird error where if i have a array of structs that is y long, when written and read from a bin file, i have to read from the y+(y-x) to get an output from the x position in the original array. its quite weird, and probably just something with macOS

Update, I think I figured it out now. I was reading with the same pointer as my writing pointer, and thus the position was all screwed up.

theupsiloninitiative
Автор

Thanks a lot! I was pretty much banging my head into the wall while trying to figure out how to do this.

I'm still a bit lost, but at least I don't feel like I'm on the wrong planet.

WhiskeredBope
Автор

I like this video and it is exactly what I am looking for. Thank you!

NTC
Автор

Thank you for a very good tutorial. Very clear explainations with no jargon. There is a big issue though because it uses the SIZE variable from when the file was created to figure out the number of students the file contains when reading from it. In practise you would need to work out the number of students the file contains from the size of the file itself.

bumblebee
Автор

I got a compiler error saying, 'write' was not declared in this scope; did you mean 'fwrite'?
Upon further research I found this... it wasn't located in fstream, but in miscellaneous functions
in a header file called #include unistd.h
ssize_t write(int fildes, const void *buf, size_t nbyte);

haroldmcbroom
Автор

VERY WELL Explained bro ... But I think you should use Mic for recording your voice ... Cheers

qanbarijazkhaki
Автор

The char[50] field was padded out to the 4byte boundary (to 52 bytes), then the other two 4byte vars were added, making 60 bytes total.
That is pretty standard, the compiler will like to make the 4byte variables aligned on the 4byte boundary of course which explains the pre-pad.

wizrom
Автор

Thanks
Can I ask you something. In case of any SIZE, how to file read?

sacphaminh
Автор

How can I read a specific record? For example @ 19:39, I only want to read the third student, instead of reading all students?

NTC
Автор

Really enjoyed that thanks. Would be great to see a follow up, for example maybe searching records, editing, deleting etc? If you wanted to say delete a student from the middle you'd need to search to find that record and then somehow copy the records either side of that to another temp array that then overwrites your original (i.e. store back to file). However if you had a massive file then I imagine that isn't very efficient - guess you'd need a different container other than an array of structs.
I've been working on an exactly similar kind of program with bank names and details (as I'm just learning c++) and am a bit stuck at the moment with that search and delete and edit of a particular record as I don't know enough yet.
great though, please keep making the videos. I like your style and laid back approach.

dalegriffiths
Автор

Awesome. I hope we can encrypt and store it in that dat file. So, that no one can understand what's in it, unless they know the encryption algorithm that we used to encrypt

psycho.u
Автор

I dont know if some will see this in time but anyway :D First of all awesome video, finally i understand how this works, but for some reason it does not work for me. I have a fairly complicated struct (with vectors and others struct in it), and for the writing it works. Buf if I want to read it back, it dumps my memory full (all the 16 GB, and then goes on and allocates Gb-s from the ssd) and I dont get any of my data back :(

kolacmate
Автор

Everything was clear, but I found the Student& s : new_students and since then I'm lost!
Can you explain as clear as usual?
Thank you!

higiniofuentes
Автор

How would one read a null terminated string from a binary file? Thanks

aubreymmotong
Автор

how to store files in binary data container files :) ?

BoshiNagare